2024-01-29 14:48:07 +08:00
|
|
|
|
import { createI18n } from 'vue-i18n'
|
|
|
|
|
|
|
|
|
|
import en from './en.json'
|
2024-01-29 19:56:25 +08:00
|
|
|
|
import zhHans from './zh-Hans.json' // 简体中文
|
2024-01-29 14:48:07 +08:00
|
|
|
|
|
|
|
|
|
const messages = {
|
|
|
|
|
en,
|
2024-01-29 19:56:25 +08:00
|
|
|
|
'zh-Hans': zhHans, // key 不能乱写,查看截图 screenshots/i18n.png
|
2024-01-29 16:59:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 14:48:07 +08:00
|
|
|
|
const i18n = createI18n({
|
2024-01-29 19:56:25 +08:00
|
|
|
|
locale: uni.getLocale(), // 获取已设置的语言,fallback 语言需要再 manifest.config.ts 中设置
|
2024-01-29 14:48:07 +08:00
|
|
|
|
messages,
|
|
|
|
|
})
|
|
|
|
|
|
2024-01-29 19:56:25 +08:00
|
|
|
|
console.log(uni.getLocale())
|
|
|
|
|
console.log(i18n.global.locale)
|
|
|
|
|
|
2024-01-29 16:59:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 非 vue 文件使用这个方法
|
|
|
|
|
* @param { string } localeKey 多语言的key,eg: "app.name"
|
|
|
|
|
*/
|
|
|
|
|
export const translate = (localeKey: string) => {
|
2024-01-29 18:59:17 +08:00
|
|
|
|
if (!localeKey) {
|
|
|
|
|
console.error(`[i18n] Function translate(), localeKey param is required`)
|
|
|
|
|
return ''
|
|
|
|
|
}
|
2024-01-29 19:56:25 +08:00
|
|
|
|
const locale = uni.getLocale()
|
|
|
|
|
console.log('locale:', locale)
|
|
|
|
|
|
2024-01-29 16:59:12 +08:00
|
|
|
|
const message = messages[locale]
|
|
|
|
|
if (Object.keys(message).includes(localeKey)) {
|
|
|
|
|
return message[localeKey]
|
|
|
|
|
}
|
|
|
|
|
return localeKey
|
|
|
|
|
}
|
2024-01-29 14:48:07 +08:00
|
|
|
|
export default i18n
|