79 lines
1.6 KiB
JavaScript
79 lines
1.6 KiB
JavaScript
/**
|
|
* 国际化混入
|
|
* 为所有页面提供国际化功能
|
|
*/
|
|
|
|
import i18n from './i18n.js'
|
|
|
|
export default {
|
|
data() {
|
|
return {
|
|
currentLanguage: 'zh'
|
|
}
|
|
},
|
|
|
|
created() {
|
|
// 初始化时设置当前语言
|
|
this.currentLanguage = i18n.getCurrentLanguage()
|
|
},
|
|
|
|
onShow() {
|
|
// 更新当前语言状态
|
|
this.currentLanguage = i18n.getCurrentLanguage()
|
|
},
|
|
|
|
methods: {
|
|
/**
|
|
* 获取翻译文本
|
|
* @param {string} key 翻译键
|
|
* @returns {string} 翻译后的文本
|
|
*/
|
|
$t(key) {
|
|
return i18n.t(key)
|
|
},
|
|
|
|
/**
|
|
* 切换语言
|
|
* @param {string} lang 语言代码 (zh/en)
|
|
*/
|
|
switchLanguage(lang) {
|
|
if (i18n.setLanguage(lang)) {
|
|
this.currentLanguage = lang
|
|
|
|
// 显示切换成功提示
|
|
const message = lang === 'zh' ? '语言已切换为中文' : 'Language switched to English'
|
|
uni.showToast({
|
|
title: message,
|
|
icon: 'success',
|
|
duration: 1500
|
|
})
|
|
|
|
// 强制更新页面
|
|
this.$forceUpdate()
|
|
|
|
// 触发全局语言切换事件
|
|
uni.$emit('languageChanged', lang)
|
|
|
|
return true
|
|
}
|
|
return false
|
|
},
|
|
|
|
/**
|
|
* 获取当前语言
|
|
* @returns {string} 当前语言代码
|
|
*/
|
|
getCurrentLanguage() {
|
|
return i18n.getCurrentLanguage()
|
|
},
|
|
|
|
/**
|
|
* 获取支持的语言列表
|
|
* @returns {Array} 语言列表
|
|
*/
|
|
getSupportedLanguages() {
|
|
return i18n.getSupportedLanguages()
|
|
}
|
|
}
|
|
}
|