From 2f4cfca86b43435362875b9f4261aa22f8830647 Mon Sep 17 00:00:00 2001 From: wswmsword Date: Wed, 9 Oct 2024 09:45:15 +0800 Subject: [PATCH] refactor: reduce redundant code --- src/utils/i18n.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/utils/i18n.ts b/src/utils/i18n.ts index c0f2219..17155ea 100644 --- a/src/utils/i18n.ts +++ b/src/utils/i18n.ts @@ -21,16 +21,11 @@ export const languageColumns: PickerColumn = [ /** 获取当前语言对应的语言包名称 */ function getI18nLocale() { const storedLocale = localStorage.getItem('language') || navigator.language - let locale = FALLBACK_LOCALE // 默认语言包 - for (const l of languageColumns) { - const value = l.value as string - if (value === storedLocale // 存在当前语言的语言包 - || value.indexOf(storedLocale) === 0 // 存在当前语言的任意地区的语言包 - ) { - locale = value - break - } - } + + const langs = languageColumns.map(v => v.value as string) + const foundLocale = langs.find(v => v === storedLocale || v.indexOf(storedLocale) === 0) // 存在当前语言的语言包 或 存在当前语言的任意地区的语言包 + const locale = foundLocale || FALLBACK_LOCALE // 若未找到,则使用 默认语言包 + document.querySelector('html').setAttribute('lang', locale) return locale }