vue使用vue-i18n實現(xiàn)國際化的實現(xiàn)代碼
需求
公司項目需要國際化,點擊按鈕切換中文/英文
1、安裝
npm install vue-i18n --save
2、注入 vue 實例中,項目中實現(xiàn)調(diào)用 api 和 模板語法
import VueI18n from 'vue-i18n'
Vue.use(VueI18n) ;
const i18n = new VueI18n({
locale: 'zh-CN', // 語言標識, 通過切換locale的值來實現(xiàn)語言切換,this.$i18n.locale
messages: {
'zh-CN': require('./common/lang/zh'), // 中文語言包
'en-US': require('./common/lang/en') // 英文語言包
}
})
new Vue({
el: '#app',
i18n, // 最后
router,
template: '<App/>',
components: { App }
})
3、對應(yīng)語言包
zh.js中文語言包:
export const lang = {
homeOverview:'首頁概覽',
firmOverview:'公司概述',
firmReports:'財務(wù)報表',
firmAppendix:'更多附錄',
firmIndex:'主要財務(wù)指標',
firmAnalysis:'對比分析',
firmNews:'新聞事件檔案',
firmOther:'其他功能',
}
en.js 英文語言包:
export const lang = {
homeOverview:'Home overview',
firmOverview:'firmOverview',
firmReports:'firmReports',
firmAppendix:'firmAppendix',
firmIndex:'firmIndex',
firmAnalysis:'firmAnalysis',
firmNews:'firmNews',
firmOther:'firmOther'
}
4、按鈕控制切換語言
this.$i18n.locale,當你賦值為‘zh-CN'時,導航欄就變成中文;當賦值為 ‘en-US'時,就變成英文:
<div class="top_btn" @click="changeLangEvent">中文</div>
changeLangEvent() {
console.log('changeLangEvent');
this.$confirm('確定切換語言嗎?', '提示', {
confirmButtonText: '確定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
if ( this.$i18n.locale === 'zh-CN' ) {
this.$i18n.locale = 'en-US';//關(guān)鍵語句
console.log('en-US')
}else {
this.$i18n.locale = 'zh-CN';//關(guān)鍵語句
console.log('zh-CN')
}
}).catch(() => {
console.log('catch');
this.$message({
type: 'info',
});
});
}
5、模板渲染
靜態(tài)渲染:
<span v-text="$t('lang .homeOverview')"></span>
<span>{{$t('lang .homeOverview')}}</span>
如果是element-ui 的,在要翻譯的前面加上冒號
比如:label="用戶姓名" 就改成 :label="$t('order.userName')"
動態(tài)渲染:
<span class="el-dropdown-link">{{navCompany}}</span>
computed:{
navCompany:function(){
if(this.nav_company){
let str = 'lang'+this.nav_company;
return this.$t(str);
}
}
},
<el-submenu
v-for="(value, title1, one) in navList"
:key="one+1"
:index="(one+1).toString()">
<template slot="title">
<router-link :to="linkList[title1]">{{ getTitle(title1) }}</router-link>
</template>
</el-submenu>
methods: {
getTitle(title){
let str = 'lang.'+title;
return this.$t(str);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于vue實現(xiàn)web端超大數(shù)據(jù)量表格的卡頓解決
這篇文章主要介紹了基于vue實現(xiàn)web端超大數(shù)據(jù)量表格的卡頓解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04
vue-cli2 構(gòu)建速度優(yōu)化的實現(xiàn)方法
這篇文章主要介紹了vue-cli2 構(gòu)建速度優(yōu)化的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
VUE安裝依賴時報錯:npm ERR! code ERESOLVE的解決
在使用npm安裝項目依賴時,有時會遇到錯誤信息 “npm ERR! code ERESOLVE”,本文就來介紹一下VUE安裝依賴時報錯的解決,具有一定的參考價值,感興趣的可以了解一下2023-10-10
element?table數(shù)據(jù)量太大導致網(wǎng)頁卡死崩潰的解決辦法
當頁面數(shù)據(jù)過多,前端渲染大量的DOM時,會造成頁面卡死問題,下面這篇文章主要給大家介紹了關(guān)于element?table數(shù)據(jù)量太大導致網(wǎng)頁卡死崩潰的解決辦法,需要的朋友可以參考下2023-02-02
vue前臺顯示500和405錯誤的解決(springboot為后臺)
這篇文章主要介紹了vue前臺顯示500和405錯誤的解決(springboot為后臺),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
手寫Vue內(nèi)置組件component的實現(xiàn)示例
本文主要介紹了手寫Vue內(nèi)置組件component的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07

