Ste7en
Ste7en
  • 发布:2018-10-17 16:49
  • 更新:2023-11-17 18:40
  • 阅读:45527

uniapp与vue-i18n实现国际化多语言

分类:uni-app

踩了很多坑,终于捣鼓出来了。

main.js

import Vue from 'vue'  
import App from './App'  
import VueI18n from 'vue-i18n'  

Vue.use(VueI18n)  
Vue.config.productuinTip = false  

const i18n = new VueI18n({  
  locale: 'en-US',  
  messages: {  
    'en-US': {  
      index: {  
        invite: 'Invite',  
        game: 'Game'  
      }  
    },  
    'zh-CN': {  
      index: {  
        invite: '邀请',  
        game: '游戏'  
      }  
    }  
  }  
})  

Vue.prototype._i18n = i18n  
App.mpType = 'app'  

const app = new Vue({  
  i18n,  
  ...App  
})  
app.$mount()

uniapp 不支持在取值表达式中直接调方法,因此,$t方法不可用,所以通过计算属性的方式:

index.vue

<template>  
  <view class="uni-content">  
    <text>{{ i18n.invite }}</text>  
    <text>{{ i18n.game }}</text>  
  </view>  
</template>  

<script>  
export default {  
  computed: {  
    i18n () {  
      return this.$t('index')  
    }  
  }  
}  
</script>  

<style>  
</style>

更多国际化参考https://ask.dcloud.net.cn/article/35872

15 关注 分享
Geeker Trust 春秋战国 sonicsunsky 虫雪浓 易齐 今天回复我了吗 9***@qq.com 没得好名字 z***@sina.com 7***@qq.com 冷月i 1***@qq.com 2***@qq.com MonikaCeng

要回复文章请先登录注册

存在的主义

存在的主义

有人遇见使用vue-i18n的时候,在真机上,是中文环境时走的英文语言包么?
模拟器上是正常的
2019-11-04 18:14
4***@qq.com

4***@qq.com

回复 4***@qq.com :
你解决了么?我也想知道怎么引入- -有点懵逼
2019-10-30 12:11
辉一

辉一

回复 1***@qq.com :
添加原型对象啊
2019-10-21 09:37
4***@qq.com

4***@qq.com

想知道怎么引入,app端 import VueI18n from 'vue-i18n' 总是报 文件查找失败
2019-10-15 16:46
1***@qq.com

1***@qq.com

回复 辉一 :
为什么加上这句就可以了啊 有啥原理?
2019-09-24 14:15
3***@qq.com

3***@qq.com

APP上会 Cannot read property '_t' of undefined 报这个错误怎么处理呢..
this.$t('index') 这句引起的~
2019-09-03 19:03
Liew

Liew

回复 辉一 :
对我当时也是少这一句:Vue.prototype._i18n = i18n
2019-08-16 14:07
辉一

辉一

回复 辉一 :
解决了。。。自己回来填坑
就差了这句Vue.prototype._i18n = i18n
2019-08-15 21:00
辉一

辉一

回复 Liew :
大哥,你这个问题得到解决了吗?我也遇到这样的坑了,救救我
2019-08-15 11:37
辉一

辉一

真机测试的这个问题困扰了我很久,求大神帮忙解决
TypeError: undefined is not an object (evaluating 'i18n._t')
2019-08-15 11:34