diff --git a/common/js/lang.js b/common/js/lang.js index 996c0ab..fbe7908 100644 --- a/common/js/lang.js +++ b/common/js/lang.js @@ -30,12 +30,12 @@ function processRoutePath(route) { } } // ---- 处理页面目录映射 ---- - + // 去掉pages目录,只保留子目录 if (routeParts[0] === 'pages') { routeParts = routeParts.slice(1); - } - + } + return routeParts.join("/"); } @@ -54,11 +54,14 @@ function loadLangPackSync(lang, path) { } } +/** + * 获得当前本地语言 + * @returns + */ function getCurrentLocale() { - return uni.getStorageSync('lang') || "zh-cn"; + return uni.getStorageSync('lang') || "zh-cn"; } - export default { langList: langConfig.langList, @@ -77,18 +80,18 @@ export default { if (!_page) return; const locale = getCurrentLocale(); // 获得当前本地语言 - + let value = ''; // 存放解析后的语言值 let langPath = ''; // 存放当前页面语言包路径 try { //公共语言包(同步加载) var lang = loadLangPackSync(locale, 'common'); - + //当前页面语言包(同步加载) let route = _page.route; langPath = processRoutePath(route); - + // 加载当前页面语言包 let currentPageLang = loadLangPackSync(locale, langPath); @@ -113,7 +116,7 @@ export default { } else { value = mergedLang[field] !== undefined ? mergedLang[field] : field; } - + } catch (e) { console.error('解析语言包失败:', e, { langPath, field, locale }); value = field; @@ -129,7 +132,7 @@ export default { // 多语言调试,注释后可以关闭控制台输出 if (field == value) { - console.warn(`警告: 字段 ${field} 在语言包 ${langPath} 中未找到对应值,使用默认值 ${field} 当前语言: ${locale}`); + console.warn(`警告: 字段 ${field} 在语言包 ${langPath} 中未找到对应值,使用默认值 ${field} 当前语言: ${locale}`); } return value; @@ -165,7 +168,7 @@ export default { if (!_page) return; const locale = getCurrentLocale(); - + this.title(this.lang("title")); //设置tabbar的文字语言 @@ -215,4 +218,4 @@ export default { } return list; } -} \ No newline at end of file +}