如何在uni-app纯js文件中使用国际化 $t()?

paddyP
- 发布:2023-07-26 15:54
- 更新:2024-12-28 09:51
- 阅读:1893



爱豆豆 - 办法总比困难多
在需要使用i18n的js里引入i18n 然后使用i18n.tc('')即可
import VueI18n from 'vue-i18n'
import messages from '../../locale/index.js'
const i18n = new VueI18n({locale: uni.getLocale(),messages})
export const getLanguage = () => {
console.log(i18n.tc('index.application-language'))
}
paddyP (作者)
感谢大佬回复,同时需要引入vue
import messages from '@/locale/index.js'
import VueI18n from 'vue-i18n'
import Vue from 'vue'
Vue.use(VueI18n)
const i18n = new VueI18n({locale: uni.getLocale(),messages})
2023-07-26 16:26
爱豆豆
回复 2***@qq.com: 我这不用引入vue就可以 能用就行 哈哈 麻烦点个赞 谢谢
2023-07-26 16:39
g***@163.com
回复 2***@qq.com: 大佬,复制你分享的内容,页面上无法使用,输出是定义key值,报错:[vue-i18n] Cannot translate the value of keypath 'common.developing'. Use the value of keypath as default.
2023-09-22 10:00
paddyP (作者)
回复 g***@163.com: 界面上用法不一样
<view class="cancel" @click="bindWXn()">{{$t('btn.ok')}}</view>
<view class="successBox" @click="bindWXy()">{{$t('btn.cancel')}}</view>
2023-10-10 10:23
s***@qq.com
你好,我在uniapp中的纯js文件中如上引入,但是报错:Cannot translate the value of keypath 'common.cancel'. Use the value of keypath as default。请问是什么原因呀
import Vue from 'vue'
import i18nConfig from '@/locale/index.js'
import VueI18n from '@/utils/vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n({locale:uni.getLocale(),i18nConfig})
2024-07-13 12:57