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

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

要回复文章请先登录注册

锦益

锦益

回复 DCloud_heavensoft :
可以了,是我的问题,二个方法冲突了,结果H5可以,APP不行,又没报错,以为方法不行。
2019-04-02 16:12
DCloud_heavensoft

DCloud_heavensoft

回复 锦益 :
可以。补充了文档
2019-04-02 07:02
锦益

锦益

这方法APP不能用吗?
2019-04-02 03:41
dc19901212

dc19901212

回复 Ste7en :
plus.nativeObj.View是只支持H5+吗?我在uniapp上好像没找到支持?
2019-03-30 11:08
Ste7en

Ste7en (作者)

回复 周建伟 :
底部 tabBar 用 plus.nativeObj.View 绘制一个 NView 即可实现国际化
2019-03-20 20:01
2***@qq.com

2***@qq.com

可以自己写一个底部导航
2019-03-04 14:55
2***@qq.com

2***@qq.com

:placeholder="i18n.bindTel"这样没有警告
2019-03-04 10:56
2***@qq.com

2***@qq.com

回复 8***@qq.com :
:placeholder="$t(i18n.account_placeholder)" 会有警告Value of key '请输入密码' is not a string!
2019-03-04 09:23
2***@qq.com

2***@qq.com

:placeholder="$t(i18n.account_placeholder)"报错
2019-03-04 09:22
沉默的大侠

沉默的大侠

回复 周建伟 :
我也想知道。。。
2019-02-01 18:19