uniapp多语言切换简化版(踩坑记录)

1.如果没有package.json则需要创建一个package.json文件,并添加代码

{
    "dependencies": {

    }
}

2.运行 npm install vue-i18n --save 

3.在根目录下添加语言包文件

json文件格式

{
    "search": "搜索",
    "place_order": "下单",
    "history": "历史",
    "order_list": "订单",
    "more": "更多",

     "index":{
        "place_order": "下单",
        "history": "历史",
        "order_list": "订单",
    } 

}

4.index.js声明语言包的别名(别名决定切换的文字,有些小伙伴切换不了的问题就在这)

import zh from './zh'
import en from './en'
import ja from './ja'

export default {
    'en': en,  // 'en'随便立命成什么都可以,但是注意切换的时候是要调用这个'en'的
    'zh': zh,
    'ja': ja
}

5.在main.js声明全局应用

import VueI18n from 'vue-i18n' //引入npm下载模块
import messages from 'lang/index.js'; //引入自定义语言文件 


Vue.use(VueI18n); //全局使用
const i18n = new VueI18n({

    // 默认选择的语言,防止手动(物理)刷新后回到默认值
    locale: uni.getStorageSync('locale') || 'en',
    messages: messages
});

Vue.prototype._i18n = i18n; //作为属性挂载

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

6.使用语言包

<template>

        <view>

                <view> {{$t('search')}} {{cs}}</view>

                <button @click="lang()">切换语言</button>

        </view>

</template>

<script> 
    export default {
        data() {
            return { 
                cs: 1, 
            }
        }, 

        methods: {
            lang() {
                if (this.cs == 1) {

                    //刷新赋值语言,'en'一定要是index.js里面立命的
                    this.$i18n.locale = 'en';

                    //保存到缓存中,防止手动(物理)刷新后语言又回到默认值
                    uni.setStorageSync('locale', 'en');
                    this.cs = 0;
                } else {
                    this.$i18n.locale = 'zh';
                    uni.setStorageSync('locale', 'zh');
                    this.cs = 1;
                }

                console.log(uni.getStorageSync('locale'))
            },

        }

}

</script>  

大功告成!!

  • 32
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-app中的setup函数中使用this.$nextTick方法来延迟到下一个DOM更新周期之后执行回调函数,与Vue中的使用方式相似。通过调用this.$nextTick方法,可以确保在数据更新后再执行回调函数,以获取更新后的数据。在Vue 3中,nextTick方法的实现相对简单,可以通过源码中的代码来理解其执行机制。首先,使用currentFlushPromise变量来表示当前正在执行的任务,如果没有正在执行的任务,则使用resolvedPromise作为返回值。如果传入了回调函数fn,那么通过p.then方法在当前任务完成后执行回调函数。同时,可以通过bind方法来绑定回调函数的this指向。最终,返回一个Promise对象,以便可以使用异步操作来处理回调函数的执行结果。总而言之,通过uni-app的this.$nextTick方法可以在组件更新后执行回调函数,以获取更新后的数据。 [2 [3123 #### 引用[.reference_title] - *1* *2* [uniapp 页面渲染完成](https://blog.csdn.net/sayyy/article/details/107785823)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *3* [解析nextTick---vue3任务调度](https://blog.csdn.net/weixin_44719258/article/details/118857388)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值