vue使用vue-i18n实现国际化的实现代码
需求公司项目需要国际化,点击按钮切换中文/英文 1、安装2、注入 vue 实例中,项目中实现调用 api 和 模板语法Vue.use(VueI18n) ;
const i18n = new VueI18n({ new Vue({ 3、对应语言包zh.js中文语言包: en.js 英文语言包: 4、按钮控制切换语言this.$i18n.locale,当你赋值为‘zh-CN'时,导航栏就变成中文;当赋值为 ‘en-US'时,就变成英文: {
if ( this.$i18n.locale === 'zh-CN' ) {
this.$i18n.locale = 'en-US';//关键语句
console.log('en-US')
}else {
this.$i18n.locale = 'zh-CN';//关键语句
console.log('zh-CN')
}
}).catch(() => {
console.log('catch');
this.$message({
type: 'info',});
});
}
5、模板渲染静态渲染:
{{$t('lang .homeOverview')}}
如果是element-ui 的,在要翻译的前面加上冒号 比如:label="用户姓名" 就改成 :label="$t('order.userName')" 动态渲染:
computed:{
navCompany:function(){
if(this.nav_company){
let str = 'lang'+this.nav_company;
return this.$t(str);
}
}
},
<router-link :to="linkList[title1]">{{ getTitle(title1) }} methods: { 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |