diff --git a/common/js/lang.js b/common/js/lang.js index e62032a..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,6 +54,14 @@ function loadLangPackSync(lang, path) { } } +/** + * 获得当前本地语言 + * @returns + */ +function getCurrentLocale() { + return uni.getStorageSync('lang') || "zh-cn"; +} + export default { langList: langConfig.langList, @@ -61,9 +69,7 @@ export default { * 获得当前本地语言 * @returns */ - getCurrentLocale() { - return uni.getStorageSync('lang') || "zh-cn"; - }, + getCurrentLocale, /** * * 解析多语言 @@ -73,7 +79,7 @@ export default { let _page = getCurrentPages()[getCurrentPages().length - 1]; if (!_page) return; - const locale = this.getCurrentLocale(); // 获得当前本地语言 + const locale = getCurrentLocale(); // 获得当前本地语言 let value = ''; // 存放解析后的语言值 let langPath = ''; // 存放当前页面语言包路径 @@ -81,11 +87,11 @@ export default { try { //公共语言包(同步加载) var lang = loadLangPackSync(locale, 'common'); - + //当前页面语言包(同步加载) let route = _page.route; langPath = processRoutePath(route); - + // 加载当前页面语言包 let currentPageLang = loadLangPackSync(locale, langPath); @@ -110,7 +116,7 @@ export default { } else { value = mergedLang[field] !== undefined ? mergedLang[field] : field; } - + } catch (e) { console.error('解析语言包失败:', e, { langPath, field, locale }); value = field; @@ -126,7 +132,7 @@ export default { // 多语言调试,注释后可以关闭控制台输出 if (field == value) { - console.warn(`警告: 字段 ${field} 在语言包 ${langPath} 中未找到对应值,使用默认值 ${field} 当前语言: ${locale}`); + console.warn(`警告: 字段 ${field} 在语言包 ${langPath} 中未找到对应值,使用默认值 ${field} 当前语言: ${locale}`); } return value; @@ -141,7 +147,7 @@ export default { if (!_page) return; uni.setStorageSync("lang", value); - const locale = this.getCurrentLocale(); + const locale = getCurrentLocale(); // 清空已加载的语言包缓存 for (let key in loadedLangPacks) { @@ -161,8 +167,8 @@ export default { let _page = getCurrentPages()[getCurrentPages().length - 1]; if (!_page) return; - const locale = this.getCurrentLocale(); - + const locale = getCurrentLocale(); + this.title(this.lang("title")); //设置tabbar的文字语言