h***@163.com
h***@163.com
  • 发布:2018-09-12 13:42
  • 更新:2020-06-15 15:14
  • 阅读:7591

uniapp与vuex-i18n多语言处理

分类:uni-app

之前一直用vue-i18n,后来发现$t始终出不来。
最后使用了vuex-i18n,$t()可以用this.$i18n.translate()来代替,总算暂时解决了多语言问题。纠结了我好久

ps:可能vue-i18n也有$t的替代方法,可是文档内容太多看花眼了

main.js

import Vue from 'vue'  
import App from './App'  

import store from './store'  
import vuexI18n from 'vuex-i18n';  

Vue.use(vuexI18n.plugin, store);  

const translationsEn = {  
    "content": "This is some {type} content"  
};  

const translationsDe = {  
    "content": "Dies ist ein toller Inhalt",  
};  

// add translations directly to the application  
Vue.i18n.add('en', translationsEn);  
Vue.i18n.add('de', translationsDe);  

// set the start locale to use  
Vue.i18n.set('de');  

Vue.config.productionTip = false  
Vue.prototype.$store = store  

App.mpType = 'app'  

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

index.vue

<template>  
    <view class="content">  
        <text class="title">{{title}}=>{{t_content}}</text>  
        <button type="primary" @tap="ddd">button</button>  
    </view>  
</template>  

<script>  
    export default {  
        data: {  
            title: 'Hello',  
            cur_lang: 'en'  
        },  
        computed:{  
            t_content(){  
                return this.$i18n.translate('content')  
            }  
        },  
        methods:{  
            ddd(){  
                console.log(1122)  
                console.log(this.$i18n.translate('content'))  
            }  
        }  
    }  
</script>  

<style>  
    .content {  
        flex: 1;  
        justify-content: center;  
        align-items: center;  
    }  

    .title {  
        font-size: 36px;  
        color: #8f8f94;  
    }  
</style>  
0 关注 分享

要回复文章请先登录注册

2***@qq.com

2***@qq.com

请问nvue文件中怎么使用呢?
2020-06-15 15:14
2***@qq.com

2***@qq.com

回复 2***@qq.com :
有没有见过这个报错呢
Uncaught TypeError: Cannot read property 'registerModule' of undefined
2020-06-04 14:55
2***@qq.com

2***@qq.com

请问 import store from './store' 引入的文件内容是什么呢
2020-06-04 14:52
g***@163.com

g***@163.com

回复 9***@qq.com :
可以在页面里 再设置 uni.setNavigationBarTitle({
title: '二狗子'
});
2019-11-21 10:15
9***@qq.com

9***@qq.com

i18n的插件从那里下载的
2019-04-01 12:19
9***@qq.com

9***@qq.com

导航的翻译是怎么处理的
2019-02-23 13:26