当前位置:网站首页>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')}
边栏推荐
- Rdkit II: use rdkit screening to screen 2D pharmacophores of chemical small molecules
- 无法一次粘贴多张图片
- 深入C语言(4)——switch的定义与使用
- Sunflower senior product director technology sharing: "how to apply national remote control" in AD domain environment
- Introduction and comparison of unicast, multicast (target broadcast, multicast), broadcast, flooding, flooding
- Why do many programmers hate pair programming?
- (codeforce547)C-Mike and Foam(质因子+容斥原理)
- 通过递归实现多级联动
- I.MX6U-驱动开发-2-LED驱动
- Bingbing learning notes: operator overloading -- implementation of date class
猜你喜欢
AI_ Drug: VAE of molecular generation model (I)
Cloudera manager platform fault repair record
Malloc C language
(2022 Hangdian multi school III) 1002 boss rush (pressure dp+ dichotomy)
实例搭建Flask服务(简易版)
Understanding of p-type problems, NP problems, NPC problems, and NP hard problems in natural computing
Ribbon principle analysis namedcontextfactory
Sleuth+zipkin to track distributed service links
exness:鸽派决议帮助黄金反弹,焦点转向美国GDP
Idea configuration web container and war packaging
随机推荐
How to judge stun protocol
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
JS regular expression finds the number of times a character (string) appears (one line of code)
Whole process record of yolov3 target detection
xxxxx
Bingbing learning notes: operator overloading -- implementation of date class
(nowcoder22529C)dinner(容斥原理+排列组合)
Asynchronous callback future mode of concurrent mode
The difference between /g /m /i of JS regular expressions
Simple use of eventbus
Arm architecture and neural network
Rdkit I: using rdkit to screen the structural characteristics of chemical small molecules
Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
代码 ~ 隐藏或禁用状态栏和虚拟按键
MOS管 —— 快速复苏应用笔记(贰)[参数与应用]
Simple understanding of Poe and UPS Technology
向日葵远程控制为何采用BGP服务器?自动最优路线、跨运营商高速传输
Microcomputer principle operation
座机的表单验证
CUB_200鸟类数据集关键点可视化