wuguangyong
wuguangyong
  • 发布:2019-02-22 14:50
  • 更新:2020-02-11 03:58
  • 阅读:8830

uni-app如何做到语言切换

分类:uni-app

uni-app怎么做中英文切换,vue有npm install vue-i18n安装插件

2019-02-22 14:50 负责人:无 分享
已邀请:
3***@qq.com

3***@qq.com

同问

b***@126.com

b***@126.com

main.js

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

Vue.use(VueI18n)    

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

Vue.prototype._i18n = i18n    
Vue.prototype.$i18nMsg = function(){  
    return i18n.messages[i18n.locale]  
}  

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

index.vue

<template>  
    <view class="content">  
        <view class="content-title">{{ i18n.index.invite }}</view>  
    </view>  
</template>  

<script>  
    export default {  
        data() {  
            return {  
                title: 'Hello'  
            }  
        },  
        computed: {  
            i18n() {  
                return this.$i18nMsg()  
            }  
        },  
        onLoad() {  
            setTimeout(() => {  
                this.$i18n.locale = 'zh-CN'  
            },2000)  
        },  
        methods: {  

        }  
    }  
</script>  

<style lang="stylus" rel="stylesheet/stylus">  
    .content-title  
        color #500778  
</style>  

稍微改进了Ste7en的方法
https://ask.dcloud.net.cn/article/35102

9***@qq.com

9***@qq.com - 专业开发直销分销类软件

npm 下载了vue-i18n import 导入了 提示 文件查找失败

  • 3***@qq.com

    不会啊,你用cnpm再试一下

    2019-04-01 18:32

1***@qq.com

1***@qq.com

为什么真机测试,对应的字段没有显示,我用chrome 浏览器测试却可以呢?

1***@qq.com

1***@qq.com

不好意思,发现问题了。在重启的时候,速度太慢了。

  • MoonKai

    怎么解决的呢

    2019-09-25 12:03

2***@qq.com

2***@qq.com - 阿伟已经死了,你挑的嘛,偶像!

Vue.prototype.$i18nMsg = function(){
return i18n.messages[i18n.locale]
}

请问 up增加这一段代码的作用是什么呢,小白有点没太看懂!??

  • yvwvv

        computed里添加   computed: {    
    i18n() {
    return this.$i18nMsg()
    }
    }

    然后就可以这样用了<view class="content-title">{{ i18n.index.invite }}</view>

    2019-12-21 11:41

4***@qq.com

4***@qq.com - 网酷开发

搞这么复杂,直接做个全局变量的数组不行吗

  • wuguangyong (作者)

    你有啥好的方法?(可以减少代码量的)

    2020-03-10 17:21

该问题目前已经被锁定, 无法添加新回复