Vue项目国际化多语言i18n

Z技术 2022年06月06日 2,364次浏览

本例使用vue cli脚手架创建,简单学习使用

1、安装i18n

  1. npm install vue-i18n@8.0.0

2,本例使用系统版本如下

  1. "dependencies": {
  2. "core-js": "^3.8.3",
  3. "vue": "^2.6.14",
  4. "vue-i18n": "^8.0.0",
  5. "vue-router": "^3.5.1",
  6. "vuex": "^3.6.2"
  7. }

3、项目main.js同级下创建language文件夹,并创建ch.ts、en.ts、index.ts

ch.ts

  1. module.exports = {
  2. message: {
  3. menu1: '主页',
  4. menu2: '关于',
  5. descript:'欢迎你使用VUE',
  6. name: '轻轻的我走了,正如我轻轻的来;我轻轻的招手,作别西天的云彩。'
  7. }
  8. }

en.ts

  1. module.exports = {
  2. message: {
  3. menu1: 'Home',
  4. menu2: 'About',
  5. descript:'Welcome to your Vue.js',
  6. 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.'
  7. }
  8. }

index.ts

  1. import Vue from 'vue';
  2. import VueI18n from 'vue-i18n'
  3. Vue.use(VueI18n)
  4. const i18n = new VueI18n({
  5. locale: 'ch', //默认显示的语言
  6. messages: {
  7. ch:require('./ch.ts'), //引入语言文件
  8. en:require('./en.ts')
  9. }
  10. })

  11. export default i18n;

4、加入main.js

  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. import router from './router'
  4. import store from './store'
  5. import i18n from './language/index.ts'

  6. Vue.config.productionTip = false

  7. new Vue({
  8. router,
  9. store,
  10. i18n,
  11. render: h => h(App)
  12. }).$mount('#app')

5、修改App.vue

  1. <template>
  2. <div id="app">
  3. <div id="choiseLan">
  4. <div class="locale-changer">
  5. <select v-model="$i18n.locale" @change="selectI18($event)">
  6. <option v-for="(lang, i) in langs" :key="`Lang${i}`" :value="lang">
  7. {{ lang }}
  8. </option>
  9. </select>
  10. </div>
  11. </div>

  12. <nav>
  13. <router-link to="/">{{$t('message.menu1')}}</router-link> |
  14. <router-link to="/about">{{$t('message.menu2')}}</router-link>
  15. </nav>
  16. <router-view/>
  17. </div>
  18. </template>

  19. <style>
  20. #choiseLan {
  21. position:absolute;
  22. margin: 1em;
  23. width: 90%;
  24. height: 2em;
  25. text-align: right;
  26. }
  27. #app {
  28. font-family: Avenir, Helvetica, Arial, sans-serif;
  29. -webkit-font-smoothing: antialiased;
  30. -moz-osx-font-smoothing: grayscale;
  31. text-align: center;
  32. color: #2c3e50;
  33. }

  34. nav {
  35. padding: 30px;
  36. }

  37. nav a {
  38. font-weight: bold;
  39. color: #2c3e50;
  40. }

  41. nav a.router-link-exact-active {
  42. color: #42b983;
  43. }
  44. </style>
  45. <script>

  46. import i18n from './language/index.ts'

  47. export default {
  48. name: 'App',
  49. data() {
  50. return {
  51. langs: ['ch', 'en']
  52. }
  53. },
  54. methods:{
  55. selectI18(e){
  56. localStorage.setItem('locale', e.target.value);
  57. }
  58. },
  59. mounted() {
  60. let lan = localStorage.getItem('locale');
  61. console.log(lan);
  62. i18n.locale = lan;
  63. }
  64. }
  65. </script>

6、components 中使用,修改HelloWorld.vue

  1. <template>
  2. <div class="hello">
  3. <h1>{{$t('message.descript')}}</h1>
  4. <div>{{$t('message.name')}}</div>
  5. <!-- <button @click="$i18n.locale = 'en'">英文</button>
  6. <button @click="$i18n.locale = 'ch'">中文</button> -->
  7. </div>
  8. </template>

  9. <script>
  10. export default {
  11. name: 'HelloWorld',
  12. props: {
  13. msg: String
  14. }
  15. }

  16. </script>

  17. <!-- Add "scoped" attribute to limit CSS to this component only -->
  18. <style scoped>
  19. h3 {
  20. margin: 40px 0 0;
  21. }
  22. ul {
  23. list-style-type: none;
  24. padding: 0;
  25. }
  26. li {
  27. display: inline-block;
  28. margin: 0 10px;
  29. }
  30. a {
  31. color: #42b983;
  32. }
  33. </style>

7、启动 yarn serve 或者npm run serve

效果:

47d539790edf4d3ea520e436d34495d2.gif


*vue3.0+使用方法类似,新版本的请自行研究!

官方地址:开始 | Vue I18n

更多信息请关注公众号:20220401152838