locale中英文
This commit is contained in:
1 parent
57fda355e6
commit
1e125254eb
3 files changed
+89
-6
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import { createI18n } from 'vue-i18n'
|
||||
import type { I18nOptions } from 'vue-i18n';
|
||||
import type { App } from 'vue';
|
||||
import { set } from 'lodash-es';
|
||||
import type { LocaleEntity } from '@/data/auto/localeEntity';
|
||||
|
||||
//这儿使用await,需要如下两步处理
|
||||
//npm install vite-plugin-top-level-await -D
|
||||
@@ -36,5 +38,38 @@ export async function setupI18n(app: App) {
|
||||
app.use(i18n);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将后端 LocaleService.FindAll() 返回的 locale 表内容合并到对应语言的翻译中,
|
||||
* 作为本地 JSON 翻译文件(lang/en、lang/zh-cn)的补充。
|
||||
* 数据映射规则:
|
||||
* Lang -> 语言 key(如 zh-cn / en)
|
||||
* GroupName -> 顶层分组(对应 json 文件名的 key,如 layout / login / request)
|
||||
* Name -> 点分隔的翻译 key(如 header.logout)
|
||||
* Value -> 翻译文本
|
||||
* 需在应用启动后调用;内部使用动态 import,避免与 utils/request 形成循环依赖。
|
||||
*/
|
||||
export async function mergeDbLocaleMessages() {
|
||||
try {
|
||||
const { default: localeService } = await import('@/service/security/localeService');
|
||||
const list = await localeService.FindAll();
|
||||
if (!list || list.length === 0) return;
|
||||
|
||||
export default i18n;
|
||||
// 按语言分组,并将 GroupName.Name 铺平成嵌套对象
|
||||
const grouped: Record<string, Record<string, any>> = {};
|
||||
list.forEach((item: LocaleEntity) => {
|
||||
if (!item.Lang || !item.Name || !item.Value) return;
|
||||
const key = item.GroupName ? `${item.GroupName}.${item.Name}` : item.Name;
|
||||
if (!grouped[item.Lang]) grouped[item.Lang] = {};
|
||||
set(grouped[item.Lang], key, item.Value);
|
||||
});
|
||||
|
||||
// 合并进对应语言的翻译,覆盖同名 key
|
||||
Object.entries(grouped).forEach(([lang, messages]) => {
|
||||
i18n.global.mergeLocaleMessage(lang, messages);
|
||||
});
|
||||
} catch (e) {
|
||||
console.warn('加载数据库翻译失败,使用本地翻译文件', e);
|
||||
}
|
||||
}
|
||||
|
||||
export default i18n;
|
||||
Reference in new issue
Block a user