refactor(lang): 优化多语言翻译函数,支持传入默认值
修改lang.js的翻译方法,支持传入默认值参数,当找不到对应翻译时使用传入的默认值;同时更新member-public.js中的调用方式,简化多语言调用代码
This commit is contained in:
@@ -542,8 +542,13 @@ export default {
|
||||
// ------- 优化2: 字段解析结果缓存(命中直接返回) -------
|
||||
const fieldCacheKey = `${locale}__${langPath}__${_overrideVersion}__${field}`;
|
||||
const cachedFieldResult = _fieldResultCache[fieldCacheKey];
|
||||
const hasDefault = arguments.length > 1 && typeof arguments[1] === 'string' && arguments[1] !== '';
|
||||
if (cachedFieldResult !== undefined) {
|
||||
let value = cachedFieldResult;
|
||||
// 没找到翻译且提供了默认值 → 使用默认值
|
||||
if (value === field && hasDefault) {
|
||||
return arguments[1];
|
||||
}
|
||||
// 带参数的翻译(如 hello {0})需要在缓存后仍做参数替换
|
||||
if (arguments.length > 1) {
|
||||
for (let i = 1; i < arguments.length; i++) {
|
||||
@@ -599,8 +604,11 @@ export default {
|
||||
// 将结果存入字段缓存(不含参数替换部分:参数每次可能不同)
|
||||
_fieldResultCache[fieldCacheKey] = value;
|
||||
|
||||
// 带参数的翻译
|
||||
if (arguments.length > 1) {
|
||||
// 没找到翻译且提供了默认值 → 使用默认值
|
||||
if (value === field && hasDefault) {
|
||||
value = arguments[1];
|
||||
} else if (arguments.length > 1) {
|
||||
// 带参数的翻译
|
||||
for (let i = 1; i < arguments.length; i++) {
|
||||
value = String(value).replace("{" + (i - 1) + "}", arguments[i]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user