当前位置:Gxlcms > JavaScript > 使用vue-i18n切换中英文的效果

使用vue-i18n切换中英文的效果

时间:2021-07-01 10:21:17 帮助过:14人阅读

这篇文章主要介绍了使用 vue-i18n 切换中英文效果,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

vue-i18n 仓库地址:https://github.com/kazupon/vue-i18n

兼容性:

支持 Vue.js 2.x 以上版本

安装方法:(此处只演示 npm)

  1. npm install vue-i18n

使用方法:

1、在 main.js 中引入 vue-i18n (前提是要先引入 vue)

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

2、准备本地的翻译信息

  1. const messages = {
  2. zh: {
  3. message: {
  4. hello: '好好学习,天天向上!'
  5. }
  6. },
  7. en: {
  8. message: {
  9. hello: 'good good study, day day up!'
  10. }
  11. }
  12. }

3、创建带有选项的 VueI18n 实例

  1. const i18n = new VueI18n({
  2. locale: 'en', // 语言标识
  3. messages
  4. })

4、把 i18n 挂载到 vue 根实例上

  1. const app = new Vue({
  2. router,
  3. i18n,
  4. ...App
  5. }).$mount('#app')

5、在 HTML 模板中使用

  1. <p id="app">
  2. <h1 style="font-size: 16px; text-align: center;">{{ $t("message.hello") }}</h1>
  3. </p>

查看运行效果:

我们刚才选定的语言标识是 “en” 英语,现在改成 “zh” 中文,并查看效果

  1. const i18n = new VueI18n({
  2. locale: 'zh', // 语言标识
  3. messages
  4. })

这样就可以轻松实现国际化了,实际开发中,页面内容肯定是很多的,我们可以把对应语言的信息保存为不同的 json对象

  1. const i18n = new VueI18n({
  2. locale: 'en', // 语言标识
  3. messages: {
  4. 'zh': require('./common/lang/zh'),
  5. 'en': require('./common/lang/en')
  6. }
  7. })

zh.js

  1. // 注意:一定是 exports,不是 export,否则会报错,报错信息是下列的中的内容不是 string
  2. module.exports = {
  3. message: {
  4. title: '运动品牌'
  5. },
  6. placeholder: {
  7. enter: '请输入您喜欢的品牌'
  8. },
  9. brands: {
  10. nike: '耐克',
  11. adi: '阿迪达斯',
  12. nb: '新百伦',
  13. ln: '李宁'
  14. }
  15. }

en.js

  1. module.exports = {
  2. message: {
  3. title: 'Sport Brands'
  4. },
  5. placeholder: {
  6. enter: 'Please type in your favorite brand'
  7. },
  8. brands: {
  9. nike: 'Nike',
  10. adi: 'Adidas',
  11. nb: 'New Banlance',
  12. ln: 'LI Ning'
  13. }
  14. }

接下来,在HTML 模板中使用,要特别注意在 js 中的国际化写法

  1. // HTML
  2. <p id="app">
  3. <p style="margin: 20px;">
  4. <h1>{{$t("message.title")}}</h1>
  5. <input style="width: 300px;" class="form-control" :placeholder="$t('placeholder.enter')">
  6. <ul>
  7. <li v-for="brand in brands">{{brand}}</li>
  8. </ul>
  9. </p>
  10. </p>
  11. // JS
  12. data () {
  13. return {
  14. brands: [this.$t('brands.nike'), this.$t('brands.adi'), this.$t('brands.nb'), this.$t('brands.ln')]
  15. }
  16. },

查看编译效果:

现在换成英文的:

在上面的操作中,我们都是通过手动修改 locale 的属性值来切换语言,实际上,更希望浏览器自动识别,这里可以借助于 cookie

1、新建一个 cookie.js 文件,用于读取cookie

  1. function getCookie(name,defaultValue) {
  2. var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  3. if (arr = document.cookie.match(reg))
  4. return unescape(arr[2]);
  5. else
  6. return defaultValue;
  7. }
  8. export {
  9. getCookie
  10. }

2、在 main.js 中引入这个js,并通过 PLAY_LANG 属性来获取浏览器的语言

  1. const i18n = new VueI18n({
  2. locale: getCookie('PLAY_LANG','zh'), // 语言标识
  3. messages: {
  4. 'zh': require('./common/lang/zh'),
  5. 'en': require('./common/lang/en')
  6. }
  7. })

这里需要注意两个极易出错的地方:

(1)、将 $t() 写成了 $()

(2)、json 中在同一个对象里有同名属性

vue-i18n 提供了一个全局配置参数叫 “locale”,通过改变 locale 的值可以实现不同语种的切换

下面的案例借用了 Element UI 的弹窗样式,上面的步骤不再赘述,直接上核心代码

  1. // template
  2. <h2>{{$t('test')}}</h2>
  3. <button type="button" class="btn btn-success" @click="changeLocale">中文/EN</button>
  4. // js方法
  5. changeLocale () {
  6. this.$confirm(this.$t('layer.toggle'), this.$t('layer.tips'), {
  7. confirmButtonText: this.$t('button.ok'),
  8. cancelButtonText: this.$t('button.cancel'),
  9. type: 'warning'
  10. }).then(() => {
  11. let locale = this.$i18n.locale
  12. locale === 'zh' ? this.$i18n.locale = 'en' : this.$i18n.locale = 'zh'
  13. }).catch(() => {
  14. this.$message({
  15. type: 'info',
  16. })
  17. })
  18. },

效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

vue-cli项目根据线上环境分别打出测试包和生产包的方法

Vue使用Redux的方法

vue路由拦截及页面跳转设置的方法介绍

以上就是使用 vue-i18n 切换中英文的效果的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行