使用uniapp开发国际化nvue编译对象

vue国际化看官网,就不重点说明

主要说下nvue官网并不明确,且如果是对象的国际化就有问题

首先看官网

<script>
  import {
    initVueI18n
  } from '@dcloudio/uni-i18n'

  // const messages = {} 此处内容省略,和 vue 全局引入的写法一致

  const { t } = initVueI18n(messages)

  export default {
    data() {
      return {
      }
    }
  }
</script>

其实就是

 index.js
import langEn from './en'
import zhHans from './zh-Hans'
 
export default {
    'en': langEn,
    'zh-Hans': zhHans
};
<script>
    import {
	    initVueI18n
	  } from '@dcloudio/uni-i18n'
	
	 import messages from '@/lang/index.js';
	
	 const { t } = initVueI18n(messages)
    
     console.log(t('messages'))
     console.log(t('grid.visibleToAll'))//不可以使用对象
</script>

这种弊端是如果是对象就有问题,无法获取,下面使用借助i18n,通过原生链方法来实现

i18n.js
    import messages from '@/lang/index.js';
	const i18nEnable = false;//自行判断
	let currentLang
	if (i18nEnable) {
		currentLang = uni.getStorageSync('CURRENT_LANG')
	} else {
		currentLang = "zh-Hans"
	}
	let i18nConfig = {
		locale: currentLang, // set locale
		messages // set locale messages
	}
	
	// #ifdef VUE2
	import Vue from 'vue'
	import VueI18n from 'vue-i18n'
	Vue.use(VueI18n)
	const i18n = new VueI18n(i18nConfig)
	// #endif
	
	// #ifdef VUE3
	import {
		createI18n
	} from 'vue-i18n'
	const i18n = createI18n(i18nConfig)
	// #endif

    export default i18n
<script>
    import i18n from '@/lang/i18n.js';
	export default {
		data() {
			return {
				$t:'',
			}
		},
		onLoad() {
			this.$t = function(key) {
				var values = [],
					len = arguments.length - 1;
				while (len-- > 0) values[len] = arguments[len + 1];
			
				return i18n._t.apply(i18n, [key, i18n.locale, i18n._getMessages(), this].concat(values))
			};
            console.log(t('grid.visibleToAll'))//可以使用对象
		}
	}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WebCsDn_TDCode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值