// 管理语言资源的对象 const languageData = {}; // 加载语言资源 function loadLanguageFun(lang) { if (!lang) { lang = "zh"; } // 如果缓存中已有该语言资源,则直接使用缓存 if (languageData[lang]) { updateElements(lang); } else { // 如果没有缓存,加载并缓存 $.getJSON(`../static/i18n/i18n_${lang}.json`, function (data) { languageData[lang] = data; updateElements(lang); }); } } // 更新页面元素内容 function updateElements(lang) { // 遍历所有带有 "i18n" 属性的元素 $("[i18n]").each(function () { const key = $(this).attr("i18n"); const value = getNestedValue(languageData[lang], key); if (value) { $(this).text(value); } // 如果元素需要动态调整字体大小 if ($(this).attr('resize-font')) { adjustFontSize($(this)); // 调整字体大小 } }); } // 递归解析嵌套 JSON 键 function getNestedValue(obj, key) { if (!key) return null; return key.split(".").reduce((o, i) => (o ? o[i] : null), obj); } // 切换语言 function changeLang(lang) { if (!lang) { lang = "zh"; // 默认语言为中文 } loadLanguageFun(lang); // 加载并更新内容 // localStorage.setItem("preferredLanguage", lang); // 将语言设置存入本地存储 localStorage.setItem("selectedLanguage", lang); } // 设置页面元素的语言(动态更新) function translateElement(selector, lang) { const key = $(selector).attr("i18n"); const value = getNestedValue(languageData[lang], key); if (value) { $(selector).text(value); } // 如果元素需要动态调整字体大小 if ($(selector).attr('resize-font')) { adjustFontSize($(selector)); // 调整字体大小 } } // 通过 key 动态更新页面元素 function updateTranslation(selector, lang, key) { const ele = $(selector); if (ele) { ele.attr("i18n", key); // 更新 i18n 属性值 translateElement(selector, lang); // 更新内容 } else { console.log("Element not found"); } } // 动态计算并调整字体大小 function adjustFontSize(ele) { const scrollWidth = ele[0].scrollWidth; const clientWidth = ele[0].clientWidth; const style = window.getComputedStyle(ele[0], null).getPropertyValue("font-size"); const fontSize = parseInt(style); if (scrollWidth > clientWidth) { let setSize = ele.attr("smallSize") || 30; let newFontSize = ((clientWidth - setSize) / scrollWidth) * (fontSize - 2); ele.css("font-size", `${newFontSize}px`); // 如果元素有line-height属性,则动态调整行高 if (ele.attr("changeLineHeight")) { ele.css("line-height", `${newFontSize + 5}px`); } ele.css("overflow-x", "hidden"); // 隐藏溢出的内容 } // 如果需要检查高度 if (ele.attr("checkHeight")) { const scrollHeight = ele[0].scrollHeight; const clientHeight = ele[0].clientHeight; if (scrollHeight > clientHeight) { let setSize = 20; let newFontSize = ((clientHeight - setSize) / scrollHeight) * fontSize; ele.css("font-size", `${newFontSize}px`); ele.css("line-height", `${newFontSize + 5}px`) } } } let lang = localStorage.getItem("selectedLanguage") || "zh"; // 获取本地存储的语言设置,默认中文 // 页面加载时设置语言 $(document).ready(function () { loadLanguageFun(lang); // 加载语言内容 }); // 弹窗文本缓存 let popupTextCache = null; // 获取弹窗文本方法 async function getPopupText(textName) { const lang = localStorage.getItem("selectedLanguage") || "zh"; // 如果尚未加载,先加载数据 if (!popupTextCache) { try { const response = await fetch('../static/i18n/popupText.json'); popupTextCache = await response.json(); } catch (error) { console.error('加载弹窗文本失败:', error); return null; } } // 获取对应语言的文本 if (popupTextCache[textName] && popupTextCache[textName][lang]) { return popupTextCache[textName][lang]; } console.warn(`未找到文本: ${textName}_${lang}`); return null; } // 在文档中暴露该方法 window.getPopupText = getPopupText;