UniApp 多语言国际化配置

265 阅读1分钟

我正在参加「掘金·启航计划」

配置多语言

可自命名,比如英文、简体、繁体,根目录新建locale文件,新建index.js

image.png

1.然后在main里面引用(取本地,默认取配置的繁体)
import messages from './locale/index'
let i18nConfig = {
	locale: uni.getStorageSync('lang') || 'ft',
	messages
}
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n(i18nConfig)

2.切换语言的时候复制这个
uni.setStorageSync('lang', 'ft')
this.$i18n.locale = 'ft'
3.需要配置pages.js,用%%的方式

image.png

4.当然切换完语言,导航栏里的语言不变,onShow里设置一下tabBar,有几个设置几个index
uni.setTabBarItem({
    index: 0,
    text: this.$t('index.title'),
})