Vue配置国际化i18n开发

i18N国际化开发很麻烦,如果不是客户需求,能不搞就不搞。

此组件基于vue 2.x,vue3.0请查看官方文档

简单使用

配置文件

i18n组件官网

如果只是配自己的就可以了,不引入Element的组件,很简单

目录结构:建议src目录下新建i18n文件夹,配置分离
在这里插入图片描述

安装

按项目管理选择npm或者yarm

npm install vue-i18n
yarn add vue-i18n

先写langs文件夹下的:
Index.js

import en from './en'
import tc from './tc'
import zhcn from './zhcn'
const messages = {
  en,
  tc,
  zhcn
}
export default messages

tc\zhcn\en,分别代表繁体中文,简体中文,英文。
内容格式是一样的,value写对应的值。如下
en.js

export default {
  message: {
    imageVerificationCode: 'imageVerificationCode',
    imageVerificationTitle: 'Can not see clearly? Change one',
    usernameInput: 'Enter username',
    passwordInput: 'Enter password',
    verificationCodeInput: 'Enter verification code',
    verifyUsername: 'Please fill in the username',
    verifyPassword: 'Please fill in the password',
    verifyverifition: 'Please fill in the code',
    hello: 'hello world',
    success: 'success'
  },
  button: {
    login: "Login",
    logout: "Logout",
    add: 'Add',
    search: 'Search',
    query: 'Search'
  }
}

zhcn.js

export default {
  message: {
    imageVerificationCode: '图片验证码',
    imageVerificationTitle: '看不清楚?换一张',
    usernameInput: '输入用户名',
    passwordInput: '输入密码',
    verificationCodeInput: '输入验证码',
    verifyUsername: '请填写用户名',
    verifyPassword: '请填写密码',
    verifyverifition: '请填写验证码',
    hello: '你好,世界',
    success: '操作成功'
  },
  button: {
    login: "登录",
    logout: "登出",
    add: '新增',
    search: '搜索',
    query: '查询'
  }
}

对比一下,需要改哪些很明显。
繁体中文不写了。

langs文件夹的结束

index.js

import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)

import messages from './langs'

// 通过选项创建 VueI18n 实例
const i18n = new VueI18n({
  locale: 'tc',//默认语言
  silentFallbackWarn: true,
  messages
});

export default i18n

main.js

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

import i18n from 'i18n/index';  //这个是绝对路径,需要配置,可以使用相对路径或者加上@/变成@/i18n/index
window.vm = new Vue({
  ...
  i18n,
  render: function (h) { return h(App) },
  ...
}).$mount('#app')

如何使用

 <h2><span>{{ $t('message.hello') }}</span></h2>
  <h2><span>{{ $t('button.login') }}</span></h2>
this.$t('message.hello')
this.$t('button.login')

这样就能拿到对应的值,英文是hello world,中文就你好世界。

切换语言:

this.$i18n.locale = 'en';
this.$i18n.locale = 'zhcn';

贴出的代码都是核心代码,看个人需求绑定事件触发。。。

配置element国际化组件

element国家化组件官网

基于以上的配置文件,需要修改的有
i18n文件夹下的index.js, 不是langs文件下的

import Vue from 'vue'
import VueI18n from 'vue-i18n'
import ElementUI from 'element-ui'
Vue.use(VueI18n)

import messages from './langs'

// 通过选项创建 VueI18n 实例
const i18n = new VueI18n({
  locale: 'tc',
  silentFallbackWarn: true,
  messages
});

Vue.use(ElementUI, {
  i18n: (key, value) => i18n.t(key, value) // 在注册Element时设置i18n的处理方法
});

export default i18n

基本上就可以了。

说明:由于版本兼容的问题,element国际化不生效,
自己配置了加入内容如下:

应用于分页的插件,同样的配置方式

en.js

export default {
  ...
  el: {
    pagination: {
      goto: 'Go to',
      pagesize: '/page',
      total: 'Total {total}',
      pageClassifier: ''
    },
  }
}

zhcn.js

export default {
  ...
  el: {
    pagination: {
      goto: '前往',
      pagesize: '条/页',
      total: '共 {total} 条',
      pageClassifier: '页'
    },
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js的多语言工具vue-i18nVue 2和Vue 3中都可以使用。在Vue 3中使用vue-i18n的方法与在Vue 2中基本相同。你可以使用vue-i18n-extract这个工具来分析你的Vue.js源代码,并提取其中的国际化文案。运行vue-i18n-extract时,它会分析你的代码,并根据配置提取出需要翻译的文本。 在Vue项目中使用vue-i18n,你需要进行一些配置。在Vue 3中,你可以在HTML中使用$t()方法来获取翻译后的文案,而在TypeScript中使用$t()方法可能会报错。这是因为在TypeScript中,你需要设置全局的$t()方法。 具体在Vue 3中使用vue-i18n的方法如下: 1. 首先,安装vue-i18n。你可以使用npm或yarn等包管理工具来安装vue-i18n。 2. 配置vue-i18n。你需要在Vue应用中添加vue-i18n配置,包括语言包、翻译文案等。 3. 在全局方法中挂载$t()方法。这步是在Vue 3中使用vue-i18n时的重点。你需要在全局方法中挂载$t()方法,以便在TypeScript中使用$t()方法时不会报错。 以上就是在Vue 3中使用vue-i18n的基本方法。通过配置和使用vue-i18n,你可以实现在Vue.js项目中的多语言支持。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue-i18n提取-使用静态分析管理vue-i18n本地化。 报告丢失和未使用的i18n条目。-Vue.js开发](https://download.csdn.net/download/weixin_42134097/19135884)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vue3中使用vue-i18n(ts中使用$t, vue3不用this)](https://blog.csdn.net/JaneLittle/article/details/127112273)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值