refactor(i18n): 根据官方提示重构 i18n,全部功能正常
This commit is contained in:
@@ -1,29 +1,21 @@
|
||||
import { createI18n } from 'vue-i18n'
|
||||
|
||||
import en from './en.json'
|
||||
import zh from './zh.json'
|
||||
import zhHans from './zh-Hans.json' // 简体中文
|
||||
|
||||
const messages = {
|
||||
en,
|
||||
zh,
|
||||
'zh-Hans': zhHans, // key 不能乱写,查看截图 screenshots/i18n.png
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
console.log(getLocale())
|
||||
|
||||
const i18n = createI18n({
|
||||
locale: getLocale(), //
|
||||
locale: uni.getLocale(), // 获取已设置的语言,fallback 语言需要再 manifest.config.ts 中设置
|
||||
messages,
|
||||
})
|
||||
|
||||
console.log(uni.getLocale())
|
||||
console.log(i18n.global.locale)
|
||||
|
||||
/**
|
||||
* 非 vue 文件使用这个方法
|
||||
* @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`)
|
||||
return ''
|
||||
}
|
||||
const locale = getLocale()
|
||||
const locale = uni.getLocale()
|
||||
console.log('locale:', locale)
|
||||
|
||||
const message = messages[locale]
|
||||
if (Object.keys(message).includes(localeKey)) {
|
||||
return message[localeKey]
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 200 KiB |
@@ -1,7 +1,7 @@
|
||||
import { createSSRApp } from 'vue'
|
||||
import App from './App.vue'
|
||||
import store from './store'
|
||||
import i18n from './locales/index'
|
||||
import i18n from './locale/index'
|
||||
import 'virtual:svg-icons-register'
|
||||
import 'virtual:uno.css'
|
||||
|
||||
|
||||
@@ -29,13 +29,13 @@
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { getLocale } from '@/locales/index'
|
||||
import i18n from '@/locale/index'
|
||||
import { testI18n } from '@/utils/index'
|
||||
|
||||
const current = ref(getLocale())
|
||||
const current = ref(uni.getLocale())
|
||||
const languages = [
|
||||
{
|
||||
value: 'zh',
|
||||
value: 'zh-Hans',
|
||||
name: '中文',
|
||||
checked: 'true',
|
||||
},
|
||||
@@ -48,8 +48,9 @@ const languages = [
|
||||
const radioChange = (evt) => {
|
||||
// console.log(evt)
|
||||
current.value = evt.detail.value
|
||||
// https://uniapp.dcloud.net.cn/api/ui/locale.html#setlocale
|
||||
// 下面2句缺一不可!!!
|
||||
uni.setLocale(evt.detail.value)
|
||||
i18n.global.locale = evt.detail.value
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -79,3 +80,4 @@ const radioChange = (evt) => {
|
||||
background-color: #bcecd1;
|
||||
}
|
||||
</style>
|
||||
@/locale/index
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { translate as t } from '@/locales/index'
|
||||
import { translate as t } from '@/locale/index'
|
||||
|
||||
/**
|
||||
* test i18n in not .vue file
|
||||
|
||||
Reference in New Issue
Block a user