经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
Vuei18n 在数组中的使用方式
来源:jb51  时间:2022/8/1 13:08:48  对本文有异议

Vuei18n 在html页面表单和js数组中使用

html的结构:

  1. <a-table :columns="columns" :dataSource="tabledata" :pagination="false">

javascript的结构:

  1. import moment from "moment";
  2. const columns = [
  3. ? {
  4. ? ? dataIndex: "name",
  5. ? ? key: "name",
  6. ? ? title: "Project Name",
  7. ? ? scopedSlots: { customRender: "name" }
  8. ? },
  9. ? {
  10. ? ? title: "Project Status",
  11. ? ? dataIndex: "status",
  12. ? ? key: "status"
  13. ? }
  14. ];
  15. export default {
  16. ? name: "Myproject",
  17. ? components: { Detail, Paydetail },
  18. ? data() {
  19. ? ? return {columns:columns}
  20. ? ? }
  21. ? ?}

数组一开始是定义在data数据外面的,添加$t(‘meunitem.project_name’)不生效,然后把数组定义在了data里面:

  1. ? data() {
  2. ? ? return {
  3. ? ? ? columns:[
  4. ? ? ? ? {
  5. ? ? ? ? ? dataIndex: "name",
  6. ? ? ? ? ? key: "name",
  7. ? ? ? ? ? title: this.$t('meunitem.project_name'),
  8. ? ? ? ? ? scopedSlots: { customRender: "name" }
  9. ? ? ? ? },
  10. ? ? ? ? {
  11. ? ? ? ? ? title: "Project Status",
  12. ? ? ? ? ? dataIndex: "status",
  13. ? ? ? ? ? key: "status"
  14. ? ? ? ? }
  15. ? ? ? ]
  16. ? ? ?}
  17. ? ? }

就可以愉快的使用了~

Vue使用i18n实现国际化

要实现多语言切换,这时候接触到国际化,前端框架无数,其中几种热门的框架都有相匹配的国际化插件工具。比如:

  • vue + vue-i18n
  • angular + angular-translate
  • react + react-intl
  • jquery + jquery.i18n.property

在实际项目中使用的前端框架为Vue,故而我们将使用vue-i18n这个插件进行国际化功能的实现。 

效果如图:

如何实现国际化

1、首先在自己的项目中安装 vue-i18n依赖包。这里使用NPM进行安装

  1. npm install vue-i18n --save-dev

2、将i18n引入vue实例中,在项目中实现调用API和模板语法。现在在main.js中引入 vue-i18n。

  1. import VueI18n from 'vue-i18n' //引入vue-i18n
  2. Vue.use(VueI18n); //通过插件的形式挂载
  3. /*---------基本使用-----------*/
  4. const i18n = new VueI18n({
  5. locale: 'CN', // 语言标识
  6. messages : {
  7. en: {
  8. message: {
  9. hello: 'hello world'
  10. }
  11. },
  12. cn: {
  13. message: {
  14. hello: '你好、世界'
  15. }
  16. }
  17. }
  18. })
  19. /*---------使用语言包-----------*/
  20. const i18n = new VueI18n({
  21. locale: 'zh', // 语言标识
  22. //this.$i18n.locale // 通过切换locale的值来实现语言切换
  23. messages: {
  24. 'zh': require('./common/lang/zh'), // 中文语言包
  25. 'en': require('./common/lang/en') // 英文语言包
  26. }
  27. })
  28. /* eslint-disable no-new */
  29. new Vue({
  30. el: '#app',
  31. i18n, //挂载到实例,一定得在这个位置,而不是comonents中
  32. template: '<App/>',
  33. components: {
  34. App
  35. }
  36. });

上面的代码正式将 vue-i18n 引入 vue 项目中,创建一个 i18n 实例对象,方便全局调用。我们通过 this.$i18n.locale 来进行语言的切换。

3、接下来我们就需要新建两个js文件(或者josn文件)作为语言包。

其中en.js语言包中代码为:

  1. module.exports = {
  2. message: {
  3. login: 'Login',
  4. Username: 'Username',
  5. Password: 'Password',
  6. Captcha: 'Captcha',
  7. Language: 'Language',
  8. zh: 'Chinese',
  9. en: 'English'
  10. }
  11. }

其中zh.js语言包中代码为:

  1. module.exports = {
  2. message: {
  3. login: '登录',
  4. Username: '用户名',
  5. Password: '密码',
  6. Captcha: '验证码',
  7. Language: '语言',
  8. zh: '中文',
  9. en: '英文'
  10. }
  11. }

最后我们只需要通过触发事件的形式,来控制 locale 的值,去调用对应的语言包就可以实现国际化啦。

4、组件中触发事件切换 locale 的值,从而实现语言的切换。

template代码:

  1. <div class="lang">
  2. <el-radio-group v-model="language" size="mini">
  3. <el-radio v-for="item of lang" :label="item.value" border>{{item.label}}</el-radio>
  4. </el-radio-group>
  5. </div>

scrpit代码:

  1. import Vue from 'vue'
  2. export default {
  3. mounted() {
  4. this.$i18n.locale === 'zh' ? this.language = 0 : this.language = 1 //数据加载时判断当前属于哪种语言,为其单选按钮赋值
  5. },
  6. data() {
  7. return {
  8. language: 0,
  9. lang: [{
  10. label: this.$t('message.zh'), //模板语法的一种
  11. value: 0
  12. }, {
  13. label: this.$t('message.en'),
  14. value: 1
  15. }],
  16. }
  17. },
  18. watch: { //侦听器
  19. language: function (val) { //侦听单选按钮的变化,从而进行切换语言
  20. val === 0 ? this.$i18n.locale = 'zh' : this.$i18n.locale = 'en';
  21. Vue.set(this.lang, 0, {label: this.$t('message.zh'), value: 0});
  22. Vue.set(this.lang, 1, {label: this.$t('message.en'), value: 1})
  23. /**
  24. this.lang: [{
  25. label: this.$t('message.zh'), //如果不使用Vue.set,也可以使用更新数据的方法
  26. value: 0
  27. }, {
  28. label: this.$t('message.en'),
  29. value: 1
  30. }]
  31. **/
  32. }
  33. },
  34. }

注意:由于 JavaScript 的限制,Vue 不能检测当前变动的数组,只渲染一次,如果数据不更新视图就不更新的组件,如果切换语言则需要更新一下数据才能切换组件内的多语言。

vue-i18n 数据渲染的模板语法

模板语法暂时分为三种:

  1. //vue组件模板的使用
  2. <div>{{$t('message.zh')}}</div>
  3. //vue组件模板数据绑定的使用
  4. <input :placeholder="$t('message.zh')"></input>
  5. //vue组件data中赋值的使用
  6. data:{
  7. msg:this.$t('message.zh');
  8. }

Element UI组件库与vue-i18n的兼容问题

由于项目中使用了Element UI组件库,它里面内置的一些文字也是需要国际化,好在Element UI是有国际化的支持。但是Element UI默认只兼容vue-i18n的5.x版本,而现在vue-i18n的版本已经到了7.x,Element UI官方文档中“国际化”一节中对此有具体说明。下面将手动设置内容贴出来:

  1. import Vue from 'vue'
  2. import ElementUI from 'element-ui'
  3. import VueI18n from 'vue-i18n'
  4. import enLocale from 'element-ui/lib/locale/lang/en' //引入Element UI的英文包
  5. import zhLocale from 'element-ui/lib/locale/lang/zh-CN' //引入Element UI的中文包
  6. Vue.use(VueI18n);
  7. Vue.use(ElementUI, {
  8. i18n: (key, value) => i18n.t(key, value)
  9. }); //兼容i18n 7.x版本设置
  10. const i18n = new VueI18n({
  11. locale: 'zh', // 语言标识
  12. messages: {
  13. zh: Object.assign(require('@/components/common/lang/zh'), zhLocale), //这里需要注意一下,是如何导入多个语言包的
  14. en: Object.assign(require('@/components/common/lang/en'), enLocale),
  15. }
  16. });

路由与面包屑导航国际化的语法问题

router.js(路由配置文件)

  1. {
  2. path: '/index',
  3. name: 'nav.Home', //直接点出对应的文字
  4. component: (resolve) => require(['@/components/index'], resolve)
  5. }

Breadcrumb.vue(面包屑导航组件)

  1. <div id="Breadcrumb">
  2. <el-breadcrumb separator-class="el-icon-arrow-right">
  3. <el-breadcrumb-item :to="{ path: '/index' }">{{$t('nav.Home')}}</el-breadcrumb-item>
  4. /*注意{{$t(item.name)}}*/
  5. <el-breadcrumb-item v-for="item in $route.matched" :to="{ path: item.path}">{{$t(item.name)}}</el-breadcrumb-item>
  6. </el-breadcrumb>
  7. </div>

项目中的国际化已做完 , 希望此篇博客能帮助到更多的人

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号