
文章目录参考资料Accept-Language提示词生成的代码参考资料https://doc.vben.pro/https://doc.vben.pro/guide/in-depth/locale.htmlAccept-Languagehttps://developer.mozilla.org/zh-CN/docs/Web/HTTP/Reference/Headers/Accept-Language提示词当前使用loadMessages加载了应用的本地语言包. 请你修改这个函数优先加载网络语言包当网络语言包不可用时再换为加载本地语言包。 网络语言包位置为http://127.0.0.1:5500/langs/**/*.json 请复用modules来确定网络语言包文件名。loadNetworkMessages返回的数据中缺少以文件名作为包开头导致数据不对。生成的代码/** 网络语言包基础地址通过 Vite proxy 代理避免跨域 */constNETWORK_LANGS_BASE_URL/langs;/** * 从网络加载语言包复用 modules 确定文件名 * param lang */asyncfunctionloadNetworkMessages(lang:SupportedLanguagesType,):PromiseRecordstring,any{// 从 modules 中提取当前语言对应的文件相对路径langs/zh-CN/page.jsonconstfilePathsObject.keys(modules).filter((key)key.startsWith(./langs/${lang}/)).map((key)key.replace(./langs/,));if(filePaths.length0){thrownewError(No network language files found for${lang});}constresponsesawaitPromise.all(filePaths.map(async(path){consturl${NETWORK_LANGS_BASE_URL}/${path};constresponseawaitfetch(url);if(!response.ok){thrownewError(Failed to fetch${url}: HTTP${response.status});}// 提取文件名不含扩展名作为键名与 loadLocalesMapFromDir 行为一致constfileNamepath.replace(/^.*\/(.)\.json$/,$1);return{fileName,data:awaitresponse.json()};}),);// 合并所有 JSON 文件为一个对象以文件名作为包名constmessages:Recordstring,any{};for(const{fileName,data}ofresponses){messages[fileName]data;}returnmessages;}/** * 加载应用特有的语言包 * 优先从网络加载http://127.0.0.1:5500/langs/ * 网络不可用时回退到本地语言包 * param lang */asyncfunctionloadMessages(lang:SupportedLanguagesType){const[appLocaleMessages]awaitPromise.all([(async(){try{constmessagesawaitloadNetworkMessages(lang);//console.warn(Successfully loaded network language files for ${lang});return{default:messages};}catch{// 网络加载失败回退到本地语言包//console.warn(网络加载失败回退到本地语言包);returnlocalesMap[lang]?.();}})(),loadThirdPartyMessage(lang),]);returnappLocaleMessages?.default;}