vue中i18n的安裝與幾種使用方式詳解
介紹 Vue I18n 是 Vue.js 的國際化插件。它可以輕松地將一些本地化功能集成到你的 Vue.js 應(yīng)用程序中。
vue中i18n安裝
1、在項目中安裝i18n
npm install vue-i18n
2、如果在一個模塊系統(tǒng)中使用它,你必須通過 Vue.use() 明確地安裝 vue-i18n:
import Vue from 'vue' import VueI18n from 'vue-i18n' Vue.use(VueI18n)
項目中的使用
我們在項目中最多的是通過標(biāo)簽切換來轉(zhuǎn)換語言的顯示,
locale是控制顯示語言的配置
this.$i18n.locale=‘cn’
下面是完整的代碼
文件夾路徑
創(chuàng)建i18n文件夾放入你需要的語言和index.js文件
index代碼
import VueI18n from 'vue-i18n' import Vue from 'vue' //引入element的語言包 import localeLib from 'element-ui/lib/locale' //本地 import enLocale from "element-ui/lib/locale/lang/en"; //英文 import zhLocale from "element-ui/lib/locale/lang/zh-CN"; //中文 // 引入需要語言包也可是js文件,export default拋出語言包的對象 import en from './lang/saas-en.json' import zh from'./lang/saas-zh-CN.json' Vue.use(VueI18n) //獲取本地選擇的語言 var lang = Cookie.get("bx_user_lang") || navigator.language || navigator.userLanguage || "en"; const i18n = new VueI18n({ locale: lang, // 語言標(biāo)識 fallbackLocale: 'zh-CN', messages: { en: Object.assign(en, enLocale), "zh-CN": Object.assign(zh, zhLocale) } }) // 設(shè)置語言 localeLib.i18n((key, value) => i18n.t(key, value)) export default i18n
main.js
import i18n from './i18n/i18n'; /* eslint-disable no-new */ new Vue({ el: '#app', router, store, i18n, //很重要,別忘記 components: { App }, template: '<App/>' })
使用方式 組件里使用
<template> <div> <span>{{$t('message.text')}}</span> //使用方式1 <p>{{title}}</p> //使用方式3 <span v-text="$t('message.text')"></span>//使用方式2 <el-select @change="langChange" placeholder="請選擇語言"> <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option> </el-select> </div> </template> <script> export default { data () { return { title:this.$t('message.text'), options:[ { value: 'cn', label: '中文' }, { value: 'en', label: 'English' } ] } }, methods: { //語言切換 langChange(e){ localStorage.setItem('lang',e); this.$i18n.locale = e; window.location.reload() } } } </script>
使用方式j(luò)s
vue3中在頁面js使用有點(diǎn)不同 $t() 會掛載到proxy上,在js中使用proxy.$t()
// 刪除人員 const delMenu = async (row) => { await deletePerson(row) proxy.$message.success(proxy.$t("person.message.success.delete")) }
如果組件里沒有proxy呢
先定義一個轉(zhuǎn)換的方法translateTitle()
創(chuàng)建一個i18n.ts,寫入下面代碼,返回一個translateTitle
import i18n from "@/i18n"; export function translateTitle(title: string) { const { t, te } = i18n.global if (te(`${title}`)) return t(`${title}`) return title; }
組件中使用,方法還是一樣,是用自己定義的方法translateText,代替了proxy.$t.代碼如下
import { translateText } from "@/utils/i18n" //在組件中引入 import { ElMessage } from 'element-plus' const tots=()=>{ ElMessage.success(translateText('person.message.success')) }
最后附上官方文檔的地址
https://kazupon.github.io/vue-i18n/zh/
總結(jié)
到此這篇關(guān)于vue中i18n的安裝與幾種使用方式的文章就介紹到這了,更多相關(guān)vue中i18n安裝與使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue基于html2canvas和jspdf?生成pdf?、解決jspdf中文亂碼問題的方法詳解
這篇文章主要介紹了vue基于html2canvas和jspdf?生成pdf?、解決jspdf中文亂碼問題的方法,結(jié)合實例形式詳細(xì)描述了中文亂碼問題的原因、解決方法與相關(guān)注意事項,需要的朋友可以參考下2023-06-06詳解刷新頁面vuex數(shù)據(jù)不消失和不跳轉(zhuǎn)頁面的解決
這篇文章主要介紹了詳解刷新頁面vuex數(shù)據(jù)不消失和不跳轉(zhuǎn)頁面的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01如何使用Vue3設(shè)計實現(xiàn)一個Model組件淺析
v-model在Vue里面是一個語法糖,數(shù)據(jù)的雙向綁定,本質(zhì)上還是通過 自定義標(biāo)簽的attribute傳遞和接受,下面這篇文章主要給大家介紹了關(guān)于如何使用Vue3設(shè)計實現(xiàn)一個Model組件的相關(guān)資料,需要的朋友可以參考下2022-08-08react+vite動態(tài)導(dǎo)入報錯@vite-ignore的問題及解決
這篇文章主要介紹了react+vite動態(tài)導(dǎo)入報錯@vite-ignore的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03vueJs函數(shù)readonly與shallowReadonly使用對比詳解
這篇文章主要為大家介紹了vueJs函數(shù)readonly與shallowReadonly使用對比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Vue $mount實戰(zhàn)之實現(xiàn)消息彈窗組件
這篇文章主要介紹了Vue $mount實現(xiàn)消息彈窗組件的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04解決element-ui?el-drawer抽屜el-dialog彈框關(guān)閉優(yōu)化demo
這篇文章主要為大家介紹了解決element-ui?el-drawer抽屜el-dialog彈框關(guān)閉優(yōu)化demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-06-06