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

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

要回复文章请先登录注册

6***@qq.com

6***@qq.com

按照这样写可以成功渲染,但是遇到一个问题,我是通过picker来切换语言,但是在手机上运行只有第一次切换语言变化了,后来切换就不生效了,页面上代码:<picker @change="bindPickerChange" :value="index" :range="language">
<view class="changeLanguage flex-rowACJS">
<i class="iconfont iconzhankai"></i>
<text>{{language[index]}}</text>
</view>
</picker>
bindPickerChange (e) {
this.index = e.detail.value
console.log(this.index === 0)
this.index === 0 ? this._i18n.locale = 'zh-CN' : this._i18n.locale = 'en-US'
console.log(this._i18n.locale)
}
烦请大神帮忙看看
2019-06-25 23:02
1***@qq.com

1***@qq.com

methods方法里怎么写
2019-06-21 17:58
9***@qq.com

9***@qq.com

更换语言后,需要重载页面吗?
2019-05-22 14:40
DCloud_heavensoft

DCloud_heavensoft

回复 Ste7en :
app名称的国际化需要离线打包,参考原生开发的做法
2019-04-24 03:40
Ste7en

Ste7en (作者)

回复 浙江移动 :
plus API 和 uni API 没有设置应用名称的API,所以名称用默认语言吧
2019-04-17 20:44
Ste7en

Ste7en (作者)

回复 tossAbout :
顶部: uni.setNavigationBarTitle({ title: this.$t('home') })

底部tabBar: uni.setTabBarItem({ index: 0, text: this.$t('home') })
2019-04-17 20:41
浙江移动

浙江移动

请问一下应用名称怎么实现国际化
2019-04-15 21:10
tossAbout

tossAbout

就是底部导航条和顶部导航条的文字怎么也实现语言切换
2019-04-15 15:48
tossAbout

tossAbout

为什么我导入
import VueI18n from 'vue-i18n'
总是报 文件查找失败
我确定我已经安装了
还有,底部导航条和顶部导航条的文字怎么修改
2019-04-15 15:47
3***@qq.com

3***@qq.com

nvue中如何用国家化呢
2019-04-09 08:32