mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-06-07 08:57:19 +08:00
perf: 优化国际化相关处理逻辑,初始化时添加缓存以避免不必要的性能消耗 (#834)
Co-authored-by: pengyuhang <pengyuhang@testbird.com>
This commit is contained in:
parent
678bd8432d
commit
792536cdea
@ -8,16 +8,20 @@ import { storageLocal, isObject } from "@pureadmin/utils";
|
|||||||
import enLocale from "element-plus/dist/locale/en.mjs";
|
import enLocale from "element-plus/dist/locale/en.mjs";
|
||||||
import zhLocale from "element-plus/dist/locale/zh-cn.mjs";
|
import zhLocale from "element-plus/dist/locale/zh-cn.mjs";
|
||||||
|
|
||||||
function siphonI18n(prefix = "zh-CN") {
|
const siphonI18n = (function () {
|
||||||
return Object.fromEntries(
|
// 仅初始化一次国际化配置
|
||||||
|
let cache = Object.fromEntries(
|
||||||
Object.entries(
|
Object.entries(
|
||||||
import.meta.glob("../../locales/*.y(a)?ml", { eager: true })
|
import.meta.glob("../../locales/*.y(a)?ml", { eager: true })
|
||||||
).map(([key, value]: any) => {
|
).map(([key, value]: any) => {
|
||||||
const matched = key.match(/([A-Za-z0-9-_]+)\./i)[1];
|
const matched = key.match(/([A-Za-z0-9-_]+)\./i)[1];
|
||||||
return [matched, value.default];
|
return [matched, value.default];
|
||||||
})
|
})
|
||||||
)[prefix];
|
);
|
||||||
}
|
return (prefix = "zh-CN") => {
|
||||||
|
return cache[prefix];
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
export const localesConfigs = {
|
export const localesConfigs = {
|
||||||
zh: {
|
zh: {
|
||||||
@ -33,7 +37,7 @@ export const localesConfigs = {
|
|||||||
/** 获取对象中所有嵌套对象的key键,并将它们用点号分割组成字符串 */
|
/** 获取对象中所有嵌套对象的key键,并将它们用点号分割组成字符串 */
|
||||||
function getObjectKeys(obj) {
|
function getObjectKeys(obj) {
|
||||||
const stack = [];
|
const stack = [];
|
||||||
const keys = [];
|
const keys: Set<string> = new Set();
|
||||||
|
|
||||||
stack.push({ obj, key: "" });
|
stack.push({ obj, key: "" });
|
||||||
|
|
||||||
@ -46,7 +50,7 @@ function getObjectKeys(obj) {
|
|||||||
if (obj[k] && isObject(obj[k])) {
|
if (obj[k] && isObject(obj[k])) {
|
||||||
stack.push({ obj: obj[k], key: newKey });
|
stack.push({ obj: obj[k], key: newKey });
|
||||||
} else {
|
} else {
|
||||||
keys.push(newKey);
|
keys.add(newKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,6 +58,17 @@ function getObjectKeys(obj) {
|
|||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 将展开的key缓存 */
|
||||||
|
const keysCache: Map<string, Set<string>> = new Map();
|
||||||
|
const flatI18n = (prefix = "zh-CN") => {
|
||||||
|
let cache = keysCache.get(prefix);
|
||||||
|
if (!cache) {
|
||||||
|
cache = getObjectKeys(siphonI18n(prefix));
|
||||||
|
keysCache.set(prefix, cache);
|
||||||
|
}
|
||||||
|
return cache;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 国际化转换工具函数(自动读取根目录locales文件夹下文件进行国际化匹配)
|
* 国际化转换工具函数(自动读取根目录locales文件夹下文件进行国际化匹配)
|
||||||
* @param message message
|
* @param message message
|
||||||
@ -73,9 +88,9 @@ export function transformI18n(message: any = "") {
|
|||||||
|
|
||||||
const key = message.match(/(\S*)\./)?.input;
|
const key = message.match(/(\S*)\./)?.input;
|
||||||
|
|
||||||
if (key && getObjectKeys(siphonI18n("zh-CN")).find(item => item === key)) {
|
if (key && flatI18n("zh-CN").has(key)) {
|
||||||
return i18n.global.t.call(i18n.global.locale, message);
|
return i18n.global.t.call(i18n.global.locale, message);
|
||||||
} else if (!key && Object.keys(siphonI18n("zh-CN")).includes(message)) {
|
} else if (!key && Object.hasOwn(siphonI18n("zh-CN"), message)) {
|
||||||
// 兼容非嵌套形式的国际化写法
|
// 兼容非嵌套形式的国际化写法
|
||||||
return i18n.global.t.call(i18n.global.locale, message);
|
return i18n.global.t.call(i18n.global.locale, message);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user