Skip to content

Commit 2f4cfca

Browse files
committed
refactor: reduce redundant code
1 parent 934ebb3 commit 2f4cfca

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/utils/i18n.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,11 @@ export const languageColumns: PickerColumn = [
2121
/** 获取当前语言对应的语言包名称 */
2222
function getI18nLocale() {
2323
const storedLocale = localStorage.getItem('language') || navigator.language
24-
let locale = FALLBACK_LOCALE // 默认语言包
25-
for (const l of languageColumns) {
26-
const value = l.value as string
27-
if (value === storedLocale // 存在当前语言的语言包
28-
|| value.indexOf(storedLocale) === 0 // 存在当前语言的任意地区的语言包
29-
) {
30-
locale = value
31-
break
32-
}
33-
}
24+
25+
const langs = languageColumns.map(v => v.value as string)
26+
const foundLocale = langs.find(v => v === storedLocale || v.indexOf(storedLocale) === 0) // 存在当前语言的语言包 或 存在当前语言的任意地区的语言包
27+
const locale = foundLocale || FALLBACK_LOCALE // 若未找到,则使用 默认语言包
28+
3429
document.querySelector('html').setAttribute('lang', locale)
3530
return locale
3631
}

0 commit comments

Comments
 (0)