139 lines
4.5 KiB
JavaScript
139 lines
4.5 KiB
JavaScript
import App from './App'
|
||
import { orderMonitor } from './utils/orderMonitor.js'
|
||
import { createSSRApp } from 'vue'
|
||
import { createI18n } from 'vue-i18n'
|
||
import zhCN from './locale/zh-CN.js'
|
||
import enUS from './locale/en-US.js'
|
||
import uView from "uview-ui"
|
||
|
||
// 获取系统语言
|
||
const getSystemLanguage = () => {
|
||
let language = 'zh-CN'
|
||
try {
|
||
const systemInfo = uni.getSystemInfoSync()
|
||
if (systemInfo && systemInfo.language) {
|
||
language = systemInfo.language === 'zh' || systemInfo.language.indexOf('zh') === 0
|
||
? 'zh-CN'
|
||
: 'en-US'
|
||
}
|
||
} catch (e) {
|
||
console.error('获取系统语言失败:', e)
|
||
}
|
||
return language
|
||
}
|
||
|
||
// 获取用户选择的语言
|
||
const getSavedLanguage = () => {
|
||
try {
|
||
const savedLang = uni.getStorageSync('language')
|
||
if (savedLang) {
|
||
return savedLang
|
||
}
|
||
const systemLang = getSystemLanguage()
|
||
uni.setStorageSync('language', systemLang)
|
||
return systemLang
|
||
} catch (e) {
|
||
console.error('语言设置出错:', e)
|
||
return 'zh-CN'
|
||
}
|
||
}
|
||
|
||
// 创建 i18n 实例(Vue 3)
|
||
let i18nInstance = null
|
||
|
||
function getI18nInstance() {
|
||
// 每次都重新读取当前语言
|
||
const currentLang = getSavedLanguage()
|
||
|
||
console.log('=== getI18nInstance 被调用 ===')
|
||
console.log('缓存中的语言:', currentLang)
|
||
console.log('当前 i18n 实例存在?', !!i18nInstance)
|
||
console.log('当前 i18n.global.locale:', i18nInstance?.global?.locale)
|
||
|
||
// 检查是否需要更新语言
|
||
if (i18nInstance && i18nInstance.global.locale !== currentLang) {
|
||
console.log('=== 检测到语言变化,强制更新 ===')
|
||
console.log('旧语言:', i18nInstance.global.locale)
|
||
console.log('新语言:', currentLang)
|
||
|
||
// 直接更新 locale(这应该会触发所有组件重新渲染)
|
||
i18nInstance.global.locale = currentLang
|
||
|
||
console.log('i18n.global.locale 已更新为:', i18nInstance.global.locale)
|
||
console.log('测试翻译 (common.loading):', i18nInstance.global.t('common.loading'))
|
||
console.log('测试翻译 (home.title):', i18nInstance.global.t('home.title'))
|
||
console.log('===================================')
|
||
|
||
return i18nInstance
|
||
}
|
||
|
||
// 首次创建实例
|
||
if (!i18nInstance) {
|
||
console.log('=== 首次创建 i18n 实例 ===')
|
||
|
||
i18nInstance = createI18n({
|
||
legacy: true, // 使用 Legacy API 模式,支持全局 $t
|
||
locale: currentLang,
|
||
fallbackLocale: 'zh-CN',
|
||
messages: {
|
||
'zh-CN': zhCN,
|
||
'en-US': enUS
|
||
},
|
||
silentTranslationWarn: true,
|
||
silentFallbackWarn: true
|
||
})
|
||
|
||
console.log('i18n 实例已创建,语言:', currentLang)
|
||
console.log('测试翻译 (common.loading):', i18nInstance.global.t('common.loading'))
|
||
console.log('测试翻译 (home.title):', i18nInstance.global.t('home.title'))
|
||
console.log('============================')
|
||
}
|
||
|
||
return i18nInstance
|
||
}
|
||
|
||
export function createApp() {
|
||
console.log('========================================')
|
||
console.log('=== createApp 被调用 ===')
|
||
console.log('时间戳:', new Date().toLocaleTimeString())
|
||
console.log('========================================')
|
||
|
||
const app = createSSRApp(App)
|
||
|
||
// 使用 uView
|
||
app.use(uView)
|
||
|
||
// 获取或更新 i18n 实例
|
||
const i18n = getI18nInstance()
|
||
|
||
// 使用 i18n
|
||
app.use(i18n)
|
||
|
||
// 手动注入 $i18n 到全局属性(确保组件可以访问)
|
||
app.config.globalProperties.$i18n = i18n.global
|
||
|
||
// 注册全局订单监控服务
|
||
app.config.globalProperties.$orderMonitor = orderMonitor
|
||
|
||
// 注册全局语言切换方法(注意:建议使用 reLaunch 来切换语言以确保完全刷新)
|
||
app.config.globalProperties.$setLanguage = (lang) => {
|
||
console.log('$setLanguage 被调用,语言:', lang)
|
||
uni.setStorageSync('language', lang)
|
||
// 更新 i18n 实例的语言
|
||
if (i18n && i18n.global) {
|
||
i18n.global.locale = lang
|
||
console.log('i18n.global.locale 已更新为:', i18n.global.locale)
|
||
}
|
||
}
|
||
|
||
console.log('=== Vue 3 应用创建完成 ===')
|
||
console.log('最终 locale:', i18n.global.locale)
|
||
console.log('app.config.globalProperties.$t 存在?', !!app.config.globalProperties.$t)
|
||
console.log('app.config.globalProperties.$i18n 存在?', !!app.config.globalProperties.$i18n)
|
||
console.log('测试 $t 调用:', i18n.global.t('common.loading'))
|
||
console.log('===========================')
|
||
|
||
return {
|
||
app
|
||
}
|
||
} |