以下语言环境信息的 ja
语言环境中不存在 message
键1
2
3
4
5
6
7
8const messages = {
en: {
message: 'hello world'
},
ja: {
// 没有翻译的本地化 `hello`
}
}
当为 VueI18n
构造函数选项指定 fallbackLocale
选项时,message
键使用 en
语言环境进行本地化:1
2
3
4
5const i18n = new VueI18n({
locale: 'ja',
fallbackLocale: 'en',
messages
})
在如下情况下1
2<p>{{ $t('message') }}</p>
this.$i18n.local = 'ja';
输出如下1
<p>hello world</p>
注意,默认情况下回退到 fallbackLocale
会产生两个控制台警告:1
2[vue-i18n] Value of key 'message' is not a string!
[vue-i18n] Fall back to translate the keypath 'message' with 'en' locale.
会在控制台产生大量警告。为了避免这些警告 (同时保留那些完全没有翻译给定关键字的警告),需初始化 VueI18n
实例时设置 silentFallbackWarn: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24import VueI18n from 'vue-i18n';
Vue.use(VueI18n);
const i18n = new VueI18n({
locale: 'en',
fallbackLocale: 'en',
silentFallbackWarn: true,
messages: {
en: require('@/assets/languages/en.js'),
hi: require('@/assets/languages/hi.js'),
id: require('@/assets/languages/id.js'),
my: require('@/assets/languages/my.js'),
es: require('@/assets/languages/es.js'),
pt: require('@/assets/languages/pt.js'),
ar: require('@/assets/languages/ar.js'),
bn: require('@/assets/languages/bn.js'),
ta: require('@/assets/languages/ta.js'),
te: require('@/assets/languages/te.js'),
ml: require('@/assets/languages/ml.js'),
mr: require('@/assets/languages/mr.js'),
pa: require('@/assets/languages/pa.js'),
gu: require('@/assets/languages/gu.js'),
kn: require('@/assets/languages/kn.js'),
},
});