import langEn from './en' import zhHans from './zh-Hans' import uniStarterConfig from '../uni-starter.config.js' const {i18n:{enable:i18nEnable} }= uniStarterConfig const messages = { 'en': langEn, 'zh-Hans': zhHans } let currentLang if(i18nEnable){ currentLang = uni.getStorageSync('CURRENT_LANG') }else{ currentLang = "zh-Hans" } // console.log(uni.getStorageSync('CURRENT_LANG'),currentLang); if (!currentLang) { if (uni.getLocale) { console.log('获取应用语言:', uni.getLocale()); let language = 'en' if (uni.getLocale() != 'en') { language = 'zh-Hans' } uni.setStorageSync('CURRENT_LANG', language) currentLang = language } else { uni.getSystemInfo({ success: function(res) { console.log('获取设备信息:', res); let language = 'zh-Hans' if (res.language == 'en') { language = 'en' } uni.setStorageSync('CURRENT_LANG', language) currentLang = language }, fail: (err) => { console.error(err) } }) } } let i18nConfig = { locale: currentLang, // set locale messages // set locale messages } // #ifdef VUE2 import Vue from 'vue' import VueI18n from 'vue-i18n' Vue.use(VueI18n) const i18n = new VueI18n(i18nConfig) // #endif // #ifdef VUE3 import { createI18n } from 'vue-i18n' const i18n = createI18n(i18nConfig) // #endif export default i18n if (i18nEnable) { console.log(` 你已开启多语言国际化,将自动根据语言获取【lang/en.js】或【lang/zh-Hans.js】文件中配置的tabbar的值, 覆盖你在pages.json中的tabbar的值 如果你不需要多语言国际化,请打开配置文件uni-starter.config.js找到 -> i18n -> enable把值设置为false `); let initLanguageAfter = () => { function $i18n(e) { try { // #ifdef VUE3 // 安全检查:确保 i18n.global 存在 if (i18n && i18n.global && i18n.global.messages && i18n.global.locale) { return i18n.global.messages[i18n.global.locale][e] } // #endif // VUE2 或 fallback if (i18n && i18n.messages && i18n.locale) { return i18n.messages[i18n.locale][e] } // 如果 i18n 未初始化,返回空字符串或默认值 return '' } catch (error) { console.error('[i18n] 获取翻译文本失败:', error) return '' } } setTimeout(function() { try { // 底部tabbar更新(仅处理国际化文本,不做权限控制) const tabbarText = $i18n('tabbar') if (tabbarText) { tabbarText.split(',').forEach((text, index) => { uni.setTabBarItem({ index, text, complete: () => {} }) }) } } catch (error) { console.error('[i18n] 更新 tabbar 失败:', error) } }, 1) } initLanguageAfter() uni.$on('changeLanguage', e => { console.log('changeLanguage', e); initLanguageAfter(e) }) }