国际化语言包配置
1.根目录创建locale文件夹,结构如下
![](https://img.haomeiwen.com/i7346101/f8dbac6bc8bf5357.png)
2.index.js代码
import en from './en.json'
import zhHans from './zh-Hans.json'
import zhHant from './zh-Hant.json'
export default {
en,
'zh-Hans': zhHans,
'zh-Hant': zhHant
}
3.语言包代码如下
![](https://img.haomeiwen.com/i7346101/a0b97dd85abd85bb.png)
3.main.js引入国际化
![](https://img.haomeiwen.com/i7346101/842b0fbbf7150f55.png)
![](https://img.haomeiwen.com/i7346101/1a46832b611058d4.png)
![](https://img.haomeiwen.com/i7346101/4a435529b625a47d.png)
使用语言包
1.在标签里面使用如下
<text class="style">{{ $t('my.lang') }}</text>
2.在js使用
this.$t('key')
3.在pages.json使用
![](https://img.haomeiwen.com/i7346101/52968934d5087971.png)
4.在外部js使用
import messages from "@/locale/index.js"
let locale = uni.getLocale() //获取当前语言
const lang = messages[locale]
accounts.message = lang['key'];
5.在NVUE 使用,computed 结合外部js使用
import messages from "@/locale/index.js"
computed: {
lang(){
let lang = uni.getLocale() //获取当前语言
return messages[lang]
}
}
<text class="text">{{ lang['key'] }}</text>
网友评论