uni-app 語(yǔ)言化 Vue-i18n

說(shuō)到vue-i18n,我們并不陌生,國(guó)際化多語(yǔ)言實(shí)現(xiàn)。

1.安裝依賴包

//vue-i18n
npm install vue-i18n
//除了安裝依賴包以外,還可以直接<script>標(biāo)簽直接引入,這種方式自行百度

2.注入vue實(shí)例中,項(xiàng)目中實(shí)現(xiàn)調(diào)用api和模板語(yǔ)法

(1)在main.js中引入vue-i18n

import Vue from 'vue'
import App from './App'
import store from './store'
import VueI18n from 'vue-i18n' 

Vue.use(VueI18n)
Vue.config.productionTip = false
Vue.prototype.$store = store

const i18n = new VueI18n({
    locale : 'en-US', //語(yǔ)言標(biāo)識(shí)
    messages: {
        'en-US' : require('common/lang/en.js') , //英文語(yǔ)言包
        'zh-CN' : require('common/lang/zh.js')  //中文繁體語(yǔ)言包
    }
})

Vue.prototype._i18n = i18n

Vue.prototype.$i18nMsg = function(){
    return i18n.messages[i18n.locale]
}

App.mpType = 'app'

const app = new Vue({
    i18n, //重要?。。?!
    store,
    ...App
})

app.$mount()

(2)en.js與zh.js

//語(yǔ)言包
export const index = {
    //英文語(yǔ)言包
    "statistics":'Statistics',
    "message":'Message',
        ......
}

(3)頁(yè)面渲染

//computed監(jiān)聽
computed:{
   i18n() {  
     return this.$i18nMsg()  
   }
},
//template中使用
{{i18n.index.statistics}}//一般
{{i18n.index[item.name]}}//v-for循環(huán)中
:placeholder="i18n.index.input" //input placeholder提示信息

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容