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