i18n 中英文翻译

45 篇文章 1 订阅
10 篇文章 0 订阅

执行以下命令安装vue 国际化插件vue-i18n

npm install vue-i18n --save

执行以下命令安装 js-cookie

npm install js-cookie --save

在src目录下 新建lang文件夹 在lang文件夹下存放 语言脚本 如下:

en.js

export default {
    // 导航栏
    linkArr: [
        {content: 'Homepage', id: 'HomePage'},
        {content: 'Product', id: 'Product'},
        {content: 'information', id: 'InforMation'},
        {content: 'AboutUs', id: 'About'},
        {content: 'JoinUs', id: 'Join'}
        // {content: 'English', id: 'English'}
    ]
}

zh.js

export default {
    // 导航栏
    linkArr: [
        {content: '首页', id: 'HomePage'},
        {content: '产品', id: 'Product'},
        {content: '资讯', id: 'InforMation'},
        {content: '关于我们', id: 'About'},
        {content: '加入我们', id: 'Join'}
        // {content: 'English', id: 'English'}
    ]
}

 index.js

import Vue from 'vue'
import VueI18n from 'vue-i18n'
import Cookies from 'js-cookie'
// import locale from 'element-ui/lib/locale';
import elementEnLocale from 'element-ui/lib/locale/lang/en' // element-ui lang
import elementZhLocale from 'element-ui/lib/locale/lang/zh-CN'// element-ui lang
import enLocale from './en'
import zhLocale from './zh'

Vue.use(VueI18n);

const messages = {
  en: {
    ...enLocale,
    ...elementEnLocale
  },
  zh: {
    ...zhLocale,
    ...elementZhLocale
  }
};
const i18n = new VueI18n({
    locale: Cookies.get('language') || 'en',
    messages
})
export default i18n

 

main.js

import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import i18n from './lang'

Vue.use(ElementUI, {
    size: 'medium',
    i18n: (key, value) => i18n.t(key, value)
});

new Vue({
    router,
    store,
    i18n,
    render: h => h(App)
}).$mount("#app");

 

 xxx.vue

<template>
   <ul>
      <router-link :to="{path: '/' + item.id}" v-for="(item,index) in $t('linkArr')" :key="index">
          <div class="line" v-if="5===index"></div>
      </router-link>
      <li @click="liClick1">
          <span>English</span>
      </li>
   </ul>
</template>

methods: {
    liClick1() {
       if (this.$i18n.locale === 'zh') {
          this.$i18n.locale = 'en';
          // 将新语言保存到全局状态管理
          this.$store.dispatch('update_current_lang', 'en');
          cookie.set('language', 'en');
        }else {
          this.$i18n.locale = 'zh';
          // 将新语言保存到全局状态管理
          this.$store.dispatch('update_current_lang', 'zh');
          cookie.set('language', 'zh');
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值