vue-i18n實現(xiàn)中英文切換的方法
1.下載
npm install vue-i18n
2.創(chuàng)建中英文包

2.1 中文包

2.2 英文包

3.在main里面引入
import VueI18n from 'vue-i18n';Vue.use(VueI18n);const i18n = new VueI18n({ locale: localStorage.getItem('lang') == (undefined || '' || null) ? 'zh' : localStorage.getItem('lang'), messages: { zh: require('../static/lang/text-zh.json'), en: require('../static/lang/text-en.json') }});new Vue({ router, store, i18n, render: h => h(App)}).$mount('#app');
4.在組件中使用
<div>{{ $t(’footer.home’) }}</div>或者<input type='span' value='' :placeholder='$t(’footer.home’)' v-model='search' />或者this.$toast(this.$t(’footer.home’))
5.使用按鈕進行手動切換,這里我用了switch用true和false來識別中英文,用這種方法也可以用于其他語言切換
<switch @change='changeEn' :checked='zhOren' />changeEn(e) { if (e.target.value) {//中文this._i18n.locale = ’zh’;localStorage.setItem(’lang’, ’zh’); } else {//英文this._i18n.locale = ’en’;localStorage.setItem(’lang’, ’en’); }}
以上就是vue-i18n實現(xiàn)中英文切換的方法的詳細內容,更多關于vue 中英文切換的資料請關注好吧啦網其它相關文章!
相關文章:
1. 詳解Android studio 動態(tài)fragment的用法2. 解決Android studio xml界面無法預覽問題3. 圖文詳解vue中proto文件的函數(shù)調用4. Spring Boot和Thymeleaf整合結合JPA實現(xiàn)分頁效果(實例代碼)5. php模擬實現(xiàn)斗地主發(fā)牌6. 什么是python的自省7. Vue封裝一個TodoList的案例與瀏覽器本地緩存的應用實現(xiàn)8. vue 使用localstorage實現(xiàn)面包屑的操作9. .Net Core使用Coravel實現(xiàn)任務調度的完整步驟10. Vuex localStorage的具體使用

網公網安備