Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit 29b3a17

Browse files
committed
⭐ new(silent): add silent translation missing option
Closes #139
1 parent 12baa02 commit 29b3a17

4 files changed

Lines changed: 55 additions & 7 deletions

File tree

decls/i18n.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ declare type I18nOptions = {
1616
missing?: MissingHandler,
1717
root?: I18n, // for internal
1818
fallbackRoot?: boolean,
19-
sync?: boolean
19+
sync?: boolean,
20+
silentTranslationWarn?: boolean
2021
};
2122

2223
declare interface I18n {
@@ -32,6 +33,8 @@ declare interface I18n {
3233
set missing (handler: MissingHandler): void,
3334
get formatter (): Formatter,
3435
set formatter (formatter: Formatter): void,
36+
get silentTranslationWarn (): boolean,
37+
set silentTranslationWarn (silent: boolean): void,
3538
getLocaleMessage (locale: Locale): LocaleMessage,
3639
setLocaleMessage (locale: Locale, message: LocaleMessage): void,
3740
t (key: Path, ...values: any): TranslateResult,

src/index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default class VueI18n {
1919
_missing: ?MissingHandler
2020
_exist: Function
2121
_watcher: any
22+
_silentTranslationWarn: boolean
2223

2324
constructor (options: I18nOptions = {}) {
2425
const locale: Locale = options.locale || 'en-US'
@@ -29,7 +30,12 @@ export default class VueI18n {
2930
this._missing = options.missing || null
3031
this._root = options.root || null
3132
this._sync = options.sync === undefined ? true : !!options.sync
32-
this._fallbackRoot = options.fallbackRoot === undefined ? true : !!options.fallbackRoot
33+
this._fallbackRoot = options.fallbackRoot === undefined
34+
? true
35+
: !!options.fallbackRoot
36+
this._silentTranslationWarn = options.silentTranslationWarn === undefined
37+
? false
38+
: !!options.silentTranslationWarn
3339

3440
this._exist = (message: Object, key: Path): boolean => {
3541
if (!message || !key) { return false }
@@ -86,12 +92,15 @@ export default class VueI18n {
8692
get formatter (): Formatter { return this._formatter }
8793
set formatter (formatter: Formatter): void { this._formatter = formatter }
8894

95+
get silentTranslationWarn (): boolean { return this._silentTranslationWarn }
96+
set silentTranslationWarn (silent: boolean): void { this._silentTranslationWarn = silent }
97+
8998
_warnDefault (locale: Locale, key: Path, result: ?any, vm: ?any): ?string {
9099
if (!isNull(result)) { return result }
91100
if (this.missing) {
92101
this.missing.apply(null, [locale, key, vm])
93102
} else {
94-
if (process.env.NODE_ENV !== 'production') {
103+
if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) {
95104
warn(
96105
`Cannot translate the value of keypath '${key}'. ` +
97106
'Use the value of keypath as default.'
@@ -116,7 +125,7 @@ export default class VueI18n {
116125
if (isPlainObject(message)) {
117126
ret = message[key]
118127
if (typeof ret !== 'string') {
119-
if (process.env.NODE_ENV !== 'production') {
128+
if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) {
120129
warn(`Value of key '${key}' is not a string!`)
121130
}
122131
return null
@@ -128,7 +137,7 @@ export default class VueI18n {
128137
if (typeof pathRet === 'string') {
129138
ret = pathRet
130139
} else {
131-
if (process.env.NODE_ENV !== 'production') {
140+
if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) {
132141
warn(`Value of key '${key}' is not a string!`)
133142
}
134143
return null
@@ -166,7 +175,7 @@ export default class VueI18n {
166175

167176
res = this._interpolate(messages[fallback], key, args)
168177
if (!isNull(res)) {
169-
if (process.env.NODE_ENV !== 'production') {
178+
if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) {
170179
warn(`Fall back to translate the keypath '${key}' with '${fallback}' locale.`)
171180
}
172181
return res
@@ -183,7 +192,7 @@ export default class VueI18n {
183192

184193
const ret: any = this._translate(messages, locale, this.fallbackLocale, key, parsedArgs.params)
185194
if (this._isFallbackRoot(ret)) {
186-
if (process.env.NODE_ENV !== 'production') {
195+
if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) {
187196
warn(`Fall back to translate the keypath '${key}' with root locale.`)
188197
}
189198
if (!this._root) { throw Error('unexpected error') }

src/mixin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export default {
5454
// component local i18n
5555
if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) {
5656
options.i18n.root = this.$root.$i18n
57+
options.i18n.silentTranslationWarn = this.$root.$i18n.silentTranslationWarn
5758
}
5859

5960
// init locale messages via custom blocks

test/unit/silent.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
describe('silent', () => {
2+
it('should be suppressed translate warnings', done => {
3+
const el = document.createElement('div')
4+
const vm = new Vue({
5+
i18n: new VueI18n({
6+
locale: 'en',
7+
silentTranslationWarn: true
8+
}),
9+
components: {
10+
child1: { // translation with component
11+
i18n: {
12+
locale: 'en',
13+
messages: {
14+
en: { who: 'child1' },
15+
ja: { who: '子1' }
16+
}
17+
},
18+
render (h) { return h('div', {}, []) }
19+
}
20+
},
21+
render (h) {
22+
return h('div', {}, [
23+
h('p', { ref: 'who' }, [this.$t('who')]),
24+
h('child1', { ref: 'child1' })
25+
])
26+
}
27+
}).$mount(el)
28+
29+
const child1 = vm.$refs.child1
30+
nextTick(() => {
31+
vm.$t('foo.bar.buz')
32+
child1.$t('foo.bar.buz')
33+
}).then(done)
34+
})
35+
})

0 commit comments

Comments
 (0)