refactor(lang): 优化多语言翻译函数,支持传入默认值

修改lang.js的翻译方法,支持传入默认值参数,当找不到对应翻译时使用传入的默认值;同时更新member-public.js中的调用方式,简化多语言调用代码
This commit is contained in:
2026-06-16 18:03:52 +08:00
parent e9ee61f509
commit c2b38476ea
2 changed files with 12 additions and 4 deletions

View File

@@ -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]);
}

View File

@@ -12,8 +12,8 @@ export default {
// 退出登录
logout() {
uni.showModal({
title: this.$lang ? this.$lang('logoutTitle') : '提示',
content: this.$lang ? this.$lang('logoutContent') : '确定要退出登录吗',
title: this.$lang('logoutTitle', '提示'),
content: this.$lang('logoutContent', '确定要退出登录吗'),
success: (res) => {
if (res.confirm) {
this.clearLoginState();