-
Notifications
You must be signed in to change notification settings - Fork 252
refactor: 重构国际化加载逻辑为 JSON 格式 #936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2153807
refactor: 重构国际化加载逻辑为 JSON 格式
RSS1102 a71a318
Merge branch 'develop' into rss1102/refactor/i18n
RSS1102 4b2e7b4
Merge branch 'develop' into rss1102/refactor/i18n
RSS1102 40d3e9e
feat(i18n): 添加国际化支持
RSS1102 de7d4c4
chore: complete i18n implementation for all hardcoded text
RSS1102 e9b3d8a
refactor: 修正 MsgDataType 类型定义
RSS1102 6808972
fix(i18n): 规范化语言代码中的连字符
RSS1102 1ba4a48
fix(i18n): 修复国际化响应式更新问题
RSS1102 9f9cc26
fix: 引入 computed 依赖
RSS1102 9cac0db
refactor(i18n): 优化语言加载与切换逻辑
RSS1102 49d1338
refactor(i18n): 规范化多语言类型处理
RSS1102 b26fcbe
refactor(i18n): 统一持久化配置中的路径字段
RSS1102 27dc55a
refactor(store): 更新 pinia persist 配置
RSS1102 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,67 +1,60 @@ | ||
| import { useLocalStorage, usePreferredLanguages } from '@vueuse/core'; | ||
| import type { DropdownOption } from 'tdesign-vue-next'; | ||
| import en_US from 'tdesign-vue-next/es/locale/en_US'; | ||
| import zh_CN from 'tdesign-vue-next/es/locale/zh_CN'; | ||
| import { computed } from 'vue'; | ||
| import type { I18nOptions } from 'vue-i18n'; | ||
| import { createI18n } from 'vue-i18n'; | ||
|
|
||
| // 导入语言文件 | ||
| const langModules = import.meta.glob('./lang/*/index.ts', { eager: true }); | ||
|
|
||
| const langModuleMap = new Map<string, unknown>(); | ||
| export const localeConfigKey = 'tdesign-starter-locale'; | ||
|
|
||
| export const langCode: Array<string> = []; | ||
| // 定义支持的语言列表,添加新语言时只需在此处添加 | ||
| export const supportedLocales = ['zh_CN', 'en_US'] as const; | ||
| export type SupportedLocale = (typeof supportedLocales)[number]; | ||
|
|
||
| export const localeConfigKey = 'tdesign-starter-locale'; | ||
| // 多语言标题类型,用于路由 meta.title 等场景 | ||
| export type LocalizedTitle = Record<SupportedLocale, string>; | ||
|
|
||
| // 获取浏览器默认语言环境 | ||
| const languages = usePreferredLanguages(); | ||
| const tdesignLocaleMap: Record<SupportedLocale, typeof zh_CN | typeof en_US> = { zh_CN, en_US }; | ||
|
|
||
| // 生成语言模块列表 | ||
| 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 langModules = import.meta.glob<{ default: Record<string, unknown> }>('./lang/*.json', { eager: true }); | ||
|
|
||
| // 导出 Message | ||
| const importMessages = computed(() => { | ||
| generateLangModuleMap(); | ||
| const langCode: SupportedLocale[] = []; | ||
| const messages: I18nOptions['messages'] = {}; | ||
| const langList: DropdownOption[] = []; | ||
|
|
||
| const message: I18nOptions['messages'] = {}; | ||
| langModuleMap.forEach((value: any, key) => { | ||
| message[key] = value.default; | ||
| }); | ||
| return message; | ||
| Object.entries(langModules).forEach(([path, module]) => { | ||
| const code = path.match(/\.\/lang\/([^.]+)\.json$/)?.[1] as SupportedLocale | undefined; | ||
| if (!code || !supportedLocales.includes(code)) return; | ||
| langCode.push(code); | ||
| messages[code] = { ...module.default, componentsLocale: tdesignLocaleMap[code] }; | ||
| langList.push({ content: module.default.lang as string, value: code }); | ||
| }); | ||
|
|
||
| export { langCode }; | ||
|
|
||
| // 获取初始语言:优先本地存储,其次浏览器偏好,最后默认中文 | ||
| const getInitialLocale = (): SupportedLocale => { | ||
| const stored = localStorage.getItem(localeConfigKey); | ||
| if (stored && supportedLocales.includes(stored as SupportedLocale)) { | ||
| return stored as SupportedLocale; | ||
| } | ||
| const preferred = navigator.languages?.[0]?.replace(/-/g, '_'); | ||
| if (preferred && supportedLocales.includes(preferred as SupportedLocale)) { | ||
| return preferred as SupportedLocale; | ||
| } | ||
| return 'zh_CN'; | ||
| }; | ||
|
|
||
| const initialLocale = getInitialLocale(); | ||
|
|
||
| export const i18n = createI18n({ | ||
| legacy: false, | ||
| locale: useLocalStorage(localeConfigKey, 'zh_CN').value || languages.value[0] || 'zh_CN', | ||
| locale: initialLocale, | ||
| fallbackLocale: 'zh_CN', | ||
| messages: importMessages.value, | ||
| messages, | ||
|
RSS1102 marked this conversation as resolved.
|
||
| globalInjection: true, | ||
| }); | ||
|
|
||
| export const langList = computed(() => { | ||
| if (langModuleMap.size === 0) generateLangModuleMap(); | ||
|
|
||
| const list: DropdownOption[] = []; | ||
| langModuleMap.forEach((value: any, key) => { | ||
| list.push({ | ||
| content: value.default.lang, | ||
| value: key, | ||
| }); | ||
| }); | ||
|
|
||
| return list; | ||
| }); | ||
|
|
||
| export const languageList = computed(() => langList); | ||
| export const { t } = i18n.global; | ||
|
|
||
| export default i18n; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.