前端国际化的踩过的坑...

需求 : 简单来说就是一键切换中英文两种状态

解决方式 : 

1,安装  vue-i18n

 npm install vue-i18n

2,将中英文js文件引入,在src下新建comment文件夹

 

如图:    

3,在main.js 引入必要的资源

import VueI18n from 'vue-i18n'
const i18n = new VueI18n({
  locale: 'zh-CN', // 语言标识
  //this.$i18n.locale, // 通过切换locale的值来实现语言切换
  messages: {
    'zh-CN': require('./common/lang/zh'), // 中文语言包
    'en-US': require('./common/lang/en') // 英文语言包
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  i18n,  // 不要忘记
  // store,
  router,
  components: { App },
  template: '<App/>'
})

4,在components组件的使用方式

例: 

<a class="h-logo" @click="change()">{{$t('m.logout')}}</a>
<span><a href="">{{$t('m.1to2Years')}}</a></span>
<span><a href="">{{$t('m.abnormalCo')}}</a></span>

data() {
  return {
    lang: 'zh-CN'
  }
},
methods: {
  //切换语言
  changeLangEvent() {
    if (this.lang === 'zh-CN') {
      this.lang = 'en-US';
      this.$i18n.locale = this.lang;
    } else {
      this.lang = 'zh-CN';
      this.$i18n.locale = this.lang;
    }
  },
 data() {
        return {
          // options: [
          //   this.$t('text.volume_24H'), this.$t('text.market')
          // ],
          lang: 'zh-CN'
        }
      },
      methods: {
        //切换语言
        changeLangEvent() {
          if (this.lang === 'zh-CN') {
            this.lang = 'en-US';
            this.$i18n.locale = this.lang;
          } else {
            this.lang = 'zh-CN';
            this.$i18n.locale = this.lang;
          }
        },

备注 :   也可以参考GitHub这个地址...

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值