|
@@ -1,29 +1,21 @@
|
|
|
import { createI18n } from 'vue-i18n'
|
|
import { createI18n } from 'vue-i18n'
|
|
|
|
|
|
|
|
import en from './en.json'
|
|
import en from './en.json'
|
|
|
-import zh from './zh.json'
|
|
|
|
|
|
|
+import zhHans from './zh-Hans.json' // 简体中文
|
|
|
|
|
|
|
|
const messages = {
|
|
const messages = {
|
|
|
en,
|
|
en,
|
|
|
- zh,
|
|
|
|
|
-}
|
|
|
|
|
-console.log(uni.getLocale())
|
|
|
|
|
-
|
|
|
|
|
-export const getLocale = () => {
|
|
|
|
|
- const browserLang = uni.getLocale()
|
|
|
|
|
- if (Object.keys(messages).includes(browserLang)) {
|
|
|
|
|
- return browserLang
|
|
|
|
|
- }
|
|
|
|
|
- return 'zh' // fallback language, 可以配置,必须是 message 的key
|
|
|
|
|
|
|
+ 'zh-Hans': zhHans, // key 不能乱写,查看截图 screenshots/i18n.png
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-console.log(getLocale())
|
|
|
|
|
-
|
|
|
|
|
const i18n = createI18n({
|
|
const i18n = createI18n({
|
|
|
- locale: getLocale(), //
|
|
|
|
|
|
|
+ locale: uni.getLocale(), // 获取已设置的语言,fallback 语言需要再 manifest.config.ts 中设置
|
|
|
messages,
|
|
messages,
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+console.log(uni.getLocale())
|
|
|
|
|
+console.log(i18n.global.locale)
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 非 vue 文件使用这个方法
|
|
* 非 vue 文件使用这个方法
|
|
|
* @param { string } localeKey 多语言的key,eg: "app.name"
|
|
* @param { string } localeKey 多语言的key,eg: "app.name"
|
|
@@ -33,7 +25,9 @@ export const translate = (localeKey: string) => {
|
|
|
console.error(`[i18n] Function translate(), localeKey param is required`)
|
|
console.error(`[i18n] Function translate(), localeKey param is required`)
|
|
|
return ''
|
|
return ''
|
|
|
}
|
|
}
|
|
|
- const locale = getLocale()
|
|
|
|
|
|
|
+ const locale = uni.getLocale()
|
|
|
|
|
+ console.log('locale:', locale)
|
|
|
|
|
+
|
|
|
const message = messages[locale]
|
|
const message = messages[locale]
|
|
|
if (Object.keys(message).includes(localeKey)) {
|
|
if (Object.keys(message).includes(localeKey)) {
|
|
|
return message[localeKey]
|
|
return message[localeKey]
|