本例使用vue cli脚手架创建,简单学习使用
1、安装i18n
- npm install vue-i18n@8.0.0
2,本例使用系统版本如下
- "dependencies": {
- "core-js": "^3.8.3",
- "vue": "^2.6.14",
- "vue-i18n": "^8.0.0",
- "vue-router": "^3.5.1",
- "vuex": "^3.6.2"
- }
3、项目main.js同级下创建language文件夹,并创建ch.ts、en.ts、index.ts
ch.ts
- module.exports = {
- message: {
- menu1: '主页',
- menu2: '关于',
- descript:'欢迎你使用VUE',
- name: '轻轻的我走了,正如我轻轻的来;我轻轻的招手,作别西天的云彩。'
- }
- }
en.ts
- module.exports = {
- message: {
- menu1: 'Home',
- menu2: 'About',
- descript:'Welcome to your Vue.js',
- name: 'Very quietly I take my leave,As quietly as I came here;Quietly I wave good-bye,To the rosy clouds in the western sky.'
- }
- }
index.ts
- import Vue from 'vue';
- import VueI18n from 'vue-i18n'
- Vue.use(VueI18n)
- const i18n = new VueI18n({
- locale: 'ch', //默认显示的语言
- messages: {
- ch:require('./ch.ts'), //引入语言文件
- en:require('./en.ts')
- }
- })
- export default i18n;
4、加入main.js
- import Vue from 'vue'
- import App from './App.vue'
- import router from './router'
- import store from './store'
- import i18n from './language/index.ts'
- Vue.config.productionTip = false
- new Vue({
- router,
- store,
- i18n,
- render: h => h(App)
- }).$mount('#app')
5、修改App.vue
- <template>
- <div id="app">
- <div id="choiseLan">
- <div class="locale-changer">
- <select v-model="$i18n.locale" @change="selectI18($event)">
- <option v-for="(lang, i) in langs" :key="`Lang${i}`" :value="lang">
- {{ lang }}
- </option>
- </select>
- </div>
- </div>
- <nav>
- <router-link to="/">{{$t('message.menu1')}}</router-link> |
- <router-link to="/about">{{$t('message.menu2')}}</router-link>
- </nav>
- <router-view/>
- </div>
- </template>
- <style>
- #choiseLan {
- position:absolute;
- margin: 1em;
- width: 90%;
- height: 2em;
- text-align: right;
- }
- #app {
- font-family: Avenir, Helvetica, Arial, sans-serif;
- -webkit-font-smoothing: antialiased;
- -moz-osx-font-smoothing: grayscale;
- text-align: center;
- color: #2c3e50;
- }
- nav {
- padding: 30px;
- }
- nav a {
- font-weight: bold;
- color: #2c3e50;
- }
- nav a.router-link-exact-active {
- color: #42b983;
- }
- </style>
- <script>
- import i18n from './language/index.ts'
- export default {
- name: 'App',
- data() {
- return {
- langs: ['ch', 'en']
- }
- },
- methods:{
- selectI18(e){
- localStorage.setItem('locale', e.target.value);
- }
- },
- mounted() {
- let lan = localStorage.getItem('locale');
- console.log(lan);
- i18n.locale = lan;
- }
- }
- </script>
6、components 中使用,修改HelloWorld.vue
- <template>
- <div class="hello">
- <h1>{{$t('message.descript')}}</h1>
- <div>{{$t('message.name')}}</div>
- <!-- <button @click="$i18n.locale = 'en'">英文</button>
- <button @click="$i18n.locale = 'ch'">中文</button> -->
- </div>
- </template>
- <script>
- export default {
- name: 'HelloWorld',
- props: {
- msg: String
- }
- }
- </script>
- <!-- Add "scoped" attribute to limit CSS to this component only -->
- <style scoped>
- h3 {
- margin: 40px 0 0;
- }
- ul {
- list-style-type: none;
- padding: 0;
- }
- li {
- display: inline-block;
- margin: 0 10px;
- }
- a {
- color: #42b983;
- }
- </style>
7、启动 yarn serve 或者npm run serve
效果:
*vue3.0+使用方法类似,新版本的请自行研究!
官方地址:开始 | Vue I18n

