-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: login view add language setting
- Loading branch information
1 parent
37d65b4
commit 4da513a
Showing
7 changed files
with
114 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,82 @@ | ||
import { useLocalStorage, usePreferredLanguages } from '@vueuse/core'; | ||
import { computed } from 'vue'; | ||
import { createI18n } from 'vue-i18n'; | ||
import { useLocalStorage, usePreferredLanguages } from '@vueuse/core' | ||
import { computed } from 'vue' | ||
import { createI18n } from 'vue-i18n' | ||
|
||
// 导入语言文件 | ||
const langModules = import.meta.glob('./lang/*/index.ts', { eager: true }) as Record<string, () => Promise<{ default: Object }>>; | ||
const langModules = import.meta.glob('./lang/*/index.ts', { eager: true }) as Record< | ||
string, | ||
() => Promise<{ default: Object }> | ||
> | ||
|
||
const langModuleMap = new Map<string, Object>(); | ||
const langModuleMap = new Map<string, Object>() | ||
|
||
export const langCode: Array<string> = []; | ||
export const langCode: Array<string> = [] | ||
|
||
export const localeConfigKey = 'MaxKB-locale'; | ||
export const localeConfigKey = 'MaxKB-locale' | ||
|
||
// 获取浏览器默认语言环境 | ||
const languages = usePreferredLanguages(); | ||
const languages = usePreferredLanguages() | ||
|
||
export function getBrowserLang() { | ||
const browserLang = navigator.language ? navigator.language : languages.value[0] | ||
let defaultBrowserLang = '' | ||
if (browserLang === 'zh-HK' || browserLang === 'zh-TW') { | ||
defaultBrowserLang = 'zh-Hant' | ||
} else if (browserLang === 'zh-CN') { | ||
defaultBrowserLang = 'zh-CN' | ||
} else { | ||
defaultBrowserLang = 'en-US' | ||
} | ||
return defaultBrowserLang | ||
} | ||
|
||
// 生成语言模块列表 | ||
const generateLangModuleMap = () => { | ||
const fullPaths = Object.keys(langModules); | ||
fullPaths.forEach((fullPath) => { | ||
const k = fullPath.replace('./lang', ''); | ||
const startIndex = 1; | ||
const lastIndex = k.lastIndexOf('/'); | ||
const code = k.substring(startIndex, lastIndex); | ||
langCode.push(code); | ||
langModuleMap.set(code, langModules[fullPath]); | ||
}); | ||
}; | ||
const fullPaths = Object.keys(langModules) | ||
fullPaths.forEach((fullPath) => { | ||
const k = fullPath.replace('./lang', '') | ||
const startIndex = 1 | ||
const lastIndex = k.lastIndexOf('/') | ||
const code = k.substring(startIndex, lastIndex) | ||
langCode.push(code) | ||
langModuleMap.set(code, langModules[fullPath]) | ||
}) | ||
} | ||
|
||
// 导出 Message | ||
const importMessages = computed(() => { | ||
generateLangModuleMap(); | ||
generateLangModuleMap() | ||
|
||
const message: Recordable = {}; | ||
langModuleMap.forEach((value: any, key) => { | ||
message[key] = value.default; | ||
}); | ||
return message; | ||
}); | ||
const message: Recordable = {} | ||
langModuleMap.forEach((value: any, key) => { | ||
message[key] = value.default | ||
}) | ||
return message | ||
}) | ||
|
||
export const i18n = createI18n({ | ||
legacy: false, | ||
locale: useLocalStorage(localeConfigKey, 'zh-CN').value || languages.value[0] || 'zh-CN', | ||
fallbackLocale: 'zh-CN', | ||
messages: importMessages.value, | ||
globalInjection: true, | ||
}); | ||
legacy: false, | ||
locale: useLocalStorage(localeConfigKey, getBrowserLang()).value || getBrowserLang(), | ||
fallbackLocale: getBrowserLang(), | ||
messages: importMessages.value, | ||
globalInjection: true | ||
}) | ||
|
||
export const langList = computed(() => { | ||
if (langModuleMap.size === 0) generateLangModuleMap(); | ||
if (langModuleMap.size === 0) generateLangModuleMap() | ||
|
||
const list:any=[] | ||
langModuleMap.forEach((value: any, key) => { | ||
list.push({ | ||
label: value.default.lang, | ||
value: key, | ||
}); | ||
}); | ||
const list: any = [] | ||
langModuleMap.forEach((value: any, key) => { | ||
list.push({ | ||
label: value.default.lang, | ||
value: key | ||
}) | ||
}) | ||
|
||
return list; | ||
}); | ||
return list | ||
}) | ||
|
||
// @ts-ignore | ||
export const { t } = i18n.global; | ||
export const { t } = i18n.global | ||
|
||
export default i18n; | ||
export default i18n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters