当前位置: 首页 > news >正文

Vue + Element UI 前端篇(五):国际化实现

Vue + Element UI 实现权限管理系统 前端篇(五):国际化实现 

国际化支持

1.安装依赖

执行以下命令,安装 i18n 依赖。

yarn add vue-i18n

复制代码

$ yarn add vue-i18n
yarn add v1.9.4
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/5] Validating package.json...
[2/5] Resolving packages...
[3/5] Fetching packages...
info fsevents@1.2.4: The platform "win32" is incompatible with this module.
info "fsevents@1.2.4" is an optional dependency and failed compatibility check. Excluding it from installation.
[4/5] Linking dependencies...
[5/5] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ vue-i18n@8.0.0
info All dependencies
└─ vue-i18n@8.0.0
Done in 28.19s.

复制代码

2.添加配置

2.1 在 src 下新建 i18n 目录,并创建一个 index.js。

index.js

复制代码

import Vue from 'vue'
import VueI18n from 'vue-i18n'Vue.use(VueI18n)// 注册i18n实例并引入语言文件,文件格式等下解析
const i18n = new VueI18n({locale: 'zh',messages: {'zh': require('@/assets/languages/zh.json'),'en': require('@/assets/languages/en.json')}
})export default i18n

复制代码

2.2 在 assets 目录下面创建连个多语言文件。

zh.json

复制代码

{"common": {"home": "首页","login": "登录","exit": "退出"},"sys": {"userMng": "用户管理","deptMng": "机构管理","roleMng": "角色管理","menuMng": "菜单管理","logMng": "日志管理"}
}

复制代码

en.json

复制代码

{"common": {"home": "Home","login": "Login","exit": "Exit"},"sys": {"userMng": "User Manage","deptMng": "Dept Manage","roleMng": "Role Manage","menuMng": "Menu Manage","logMng": "Log Manage"}
}

复制代码

2.3 在 main.js 中引入 i18n 并注入到 vue 对象中。

复制代码

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 api from './http/index'
import i18n from './i18n'Vue.config.productionTip = falseVue.use(ElementUI)
Vue.use(api)new Vue({el: '#app',i18n,router,render: h => h(App)
});

复制代码

3.字符引用

在原本使用字符串的地方,引入国际化字符串。

把原本的“用户管理”、“菜单管理”等字符串换成如下格式引入。

 

3.切换菜单

在用户信息前边添加一个用于语言切换的菜单,用于切换不同的语言。

菜单语言切换的时候,修改国际化的设置

4.启动测试

 选择点击切换英文,导航菜单成功切换到英文。

http://www.lryc.cn/news/156366.html

相关文章:

  • [ROS]虚拟机ubuntu18.04系统里面运行usb_cam
  • 常用通讯协议比较
  • 鼠标键盘管理 ShareMouse for Mac最新
  • 【ALM工具软件】上海道宁与Perforce为您带来用于整个生命周期的应用程序生命周期管理软件
  • Android图形-架构2
  • 文字验证码:简单有效的账号安全守卫!
  • Shell 运算符及语法结构
  • 为什么要建前缀索引?
  • vite介绍
  • 2023年软件测试常见面试题
  • 关于const指针的一个常见误区
  • @alilclowcode-engine-ext@1.0.5 不支持安装react@^16.3.0
  • Python之列表操作和内存模型
  • 实习面试记录
  • 总结/笔记-vue中的插槽(默认插槽、具名插槽、作用域插槽)
  • QTday5(QT连接TCP通信)
  • 【Docker】安装RabbitMQ
  • 【如何获取数据库表的字段并拼接】
  • Oracle中LEFT JOIN后AND与WHERE的异同
  • Flink实时计算中台Kubernates功能改造点
  • GO远程构建并调试
  • react使用hook封装一个search+input+checkbox组件
  • 【6】uniform颜色写入
  • 自然语言处理历史史诗:NLP的范式演变与Python全实现
  • 网络协议从入门到底层原理学习(二)—— Mac地址/IP地址
  • 2023开学礼中国海洋大学《乡村振兴战略下传统村落文化旅游设计》许少辉新海洋图书馆
  • WebClient vs HttpClient:异同对比
  • ES6中导入import导出export
  • 【MySQlL学习笔记】(九)内外连接
  • 敦煌https证书能做些什么