当前位置:网站首页>Uni app internationalization
Uni app internationalization
2022-07-29 03:42:00 【linzhiji】
Reference resources
Add folder and path

index.js
// internationalization json file , See the following example for the contents of the file
import en from './en.json'
import zhHans from './zh-Hans.json' // Chinese simplified
import zhHant from './zh-Hant.json' // Chinese traditional
export default {
en,
'zh-Hans': zhHans,
'zh-Hant': zhHant,
}
main.js
// Added content
// internationalization
import messages from './locale/index'
import {getLang} from "./utils/lang";
let i18nConfig = {
locale: getLang(),
messages
}
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n(i18nConfig)
const app = new Vue({
i18n,
store,
...App
});
Dynamic language switching


lang.vue
<template>
<view class="body">
<!-- <u-cell-item icon="setting-fill" title=" Personal settings "></u-cell-item> -->
<u-cell-item title="English" :arrow="false" @click="changeLang('en')">
<u-icon slot="right-icon" size="32" name="checkmark" v-if="isEn"></u-icon>
</u-cell-item>
<u-cell-item title=" chinese " :arrow="false" @click="changeLang('zh-Hans')">
<u-icon slot="right-icon" size="32" name="checkmark" v-if="isZh"></u-icon>
</u-cell-item>
</view>
</template>
<script>
import {getLang, setLang} from "@/utils/lang.js";
export default {
data() {
return {
isEn: false,
isZh: false
};
},
onLoad() {
console.log(getLang())
let lang=getLang()
this.updateLang(lang)
},
methods: {
updateLang(srcLang){
if(srcLang == "zh-Hans"){
this.isEn=false
this.isZh=true
}else if(srcLang == "en"){
this.isEn=true
this.isZh=false
}
},
changeLang(srcLange){
setLang(srcLange)
console.log("change language:" + srcLange)
this.updateLang(srcLange)
}
}
}
</script>
<style lang="scss" scoped>
</style>
Use
vue page
import {initVueI18n} from '@dcloudio/uni-i18n'
import messages from '../../locale/index';
const { t } = initVueI18n(messages)#vue In the page
{
{$t('index.title')}}
#page page
"navigationBarTitleText": "%index.language%",
#page page , label <>
<u-input v-model="content" type="textarea" height="250" auto-height :placeholder="$t('wit.publish_tip')"/>
// 1. vue Use of component templates
<div>{
{$t('message.zh')}}</div>
// 2. vue Use of component template data binding
<input :placeholder="$t('message.zh')"/>
// 3.vue Components data The use of assignment in
data:{
msg:this.$t('message.zh');
}nvue page
nvue Page references i18n_Dai_ Generations of blogs -CSDN Blog
stay app.vue Inside
globalData: {
$t: ''
},
onLaunch(){
this.globalData.$t = val => {
return this.$t(val);
};
},stay nvue Page using
<view>{
{ $t('conn.phone') }}</view>
# stay script
let app = getApp().globalData;
methods:{
$t(text) {
return app.$t(text);
},
}
#nvue Text
{
{$t('tab.friend')}边栏推荐
- Shortcut key for adjusting terminal size in ROS
- Complexity analysis learning
- Why do many programmers hate pair programming?
- How fast does it take to implement a super simple language
- Install the packet capturing certificate
- Simple code implementation of decision tree
- three.js 第五十四用如何给shader传递结构体数组
- 3.2 model saving and loading
- Exness: dove resolution helped gold rebound, and the focus turned to U.S. GDP
- Li Kou daily question - day 44 -205. Isomorphic string
猜你喜欢

深入C语言(3)—— C的输入输出流

(nowcoder22529c) diner (inclusion exclusion principle + permutation and combination)

Bingbing learning notes: operator overloading -- implementation of date class

1. Mx6u driver development-2-led driver

Violence recursion to dynamic programming 01 (robot movement)

Shutter start white screen

Machine learning based on deepchem

Tonight at 7:30 | is the AI world in the eyes of Lianjie, Jiangmen, Baidu and country garden venture capital continue to be advanced or return to the essence of business

Excel拼接数据库语句

深入C语言(1)——操作符与表达式
随机推荐
Rdkit: introduce smiles code, smart code and Morgan fingerprint (ECFP)
Process tracking of ribbon principle
2. 变量及作用域
Simple use of eventbus
KNN method predicts pregnancy, KNN principle simple code
Excel splicing database statement
Functions and comparison of repeaters, hubs, bridges, switches and routers
《陌路曾相逢》夏陌沈疏晏_夏陌沈疏晏最新章节
Deep into C language (3) -- input and output stream of C
Deep into C language (1) -- operators and expressions
Naive Bayes -- continuous data
Notes on letter symbol marking of papers
Build redis environment under windows and Linux
1.6 example: cifar-10 classification
Excel拼接数据库语句
Shortcut key for adjusting terminal size in ROS
RTP send and receive h265
1.4 nn. Module neural network (II)
暴力递归到动态规划 01 (机器人移动)
Configure vscade to realize ROS writing