Vue项目国际化多语言i18n

1 篇文章 0 订阅

本例使用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

更多信息请查看 博客 或者关注公众号:Z技术

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值