diff --git a/decls/i18n.js b/decls/i18n.js index 7e6b18ba1..7087c9835 100644 --- a/decls/i18n.js +++ b/decls/i18n.js @@ -16,7 +16,8 @@ declare type I18nOptions = { missing?: MissingHandler, root?: I18n, // for internal fallbackRoot?: boolean, - sync?: boolean + sync?: boolean, + silentTranslationWarn?: boolean }; declare interface I18n { @@ -32,6 +33,8 @@ declare interface I18n { set missing (handler: MissingHandler): void, get formatter (): Formatter, set formatter (formatter: Formatter): void, + get silentTranslationWarn (): boolean, + set silentTranslationWarn (silent: boolean): void, getLocaleMessage (locale: Locale): LocaleMessage, setLocaleMessage (locale: Locale, message: LocaleMessage): void, t (key: Path, ...values: any): TranslateResult, diff --git a/src/index.js b/src/index.js index f0ef34c07..33ea508fc 100644 --- a/src/index.js +++ b/src/index.js @@ -19,6 +19,7 @@ export default class VueI18n { _missing: ?MissingHandler _exist: Function _watcher: any + _silentTranslationWarn: boolean constructor (options: I18nOptions = {}) { const locale: Locale = options.locale || 'en-US' @@ -29,7 +30,12 @@ export default class VueI18n { this._missing = options.missing || null this._root = options.root || null this._sync = options.sync === undefined ? true : !!options.sync - this._fallbackRoot = options.fallbackRoot === undefined ? true : !!options.fallbackRoot + this._fallbackRoot = options.fallbackRoot === undefined + ? true + : !!options.fallbackRoot + this._silentTranslationWarn = options.silentTranslationWarn === undefined + ? false + : !!options.silentTranslationWarn this._exist = (message: Object, key: Path): boolean => { if (!message || !key) { return false } @@ -86,12 +92,15 @@ export default class VueI18n { get formatter (): Formatter { return this._formatter } set formatter (formatter: Formatter): void { this._formatter = formatter } + get silentTranslationWarn (): boolean { return this._silentTranslationWarn } + set silentTranslationWarn (silent: boolean): void { this._silentTranslationWarn = silent } + _warnDefault (locale: Locale, key: Path, result: ?any, vm: ?any): ?string { if (!isNull(result)) { return result } if (this.missing) { this.missing.apply(null, [locale, key, vm]) } else { - if (process.env.NODE_ENV !== 'production') { + if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) { warn( `Cannot translate the value of keypath '${key}'. ` + 'Use the value of keypath as default.' @@ -116,7 +125,7 @@ export default class VueI18n { if (isPlainObject(message)) { ret = message[key] if (typeof ret !== 'string') { - if (process.env.NODE_ENV !== 'production') { + if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) { warn(`Value of key '${key}' is not a string!`) } return null @@ -128,7 +137,7 @@ export default class VueI18n { if (typeof pathRet === 'string') { ret = pathRet } else { - if (process.env.NODE_ENV !== 'production') { + if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) { warn(`Value of key '${key}' is not a string!`) } return null @@ -166,7 +175,7 @@ export default class VueI18n { res = this._interpolate(messages[fallback], key, args) if (!isNull(res)) { - if (process.env.NODE_ENV !== 'production') { + if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) { warn(`Fall back to translate the keypath '${key}' with '${fallback}' locale.`) } return res @@ -183,7 +192,7 @@ export default class VueI18n { const ret: any = this._translate(messages, locale, this.fallbackLocale, key, parsedArgs.params) if (this._isFallbackRoot(ret)) { - if (process.env.NODE_ENV !== 'production') { + if (process.env.NODE_ENV !== 'production' && !this._silentTranslationWarn) { warn(`Fall back to translate the keypath '${key}' with root locale.`) } if (!this._root) { throw Error('unexpected error') } diff --git a/src/mixin.js b/src/mixin.js index 74b005597..63c86f646 100644 --- a/src/mixin.js +++ b/src/mixin.js @@ -54,6 +54,7 @@ export default { // component local i18n if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) { options.i18n.root = this.$root.$i18n + options.i18n.silentTranslationWarn = this.$root.$i18n.silentTranslationWarn } // init locale messages via custom blocks diff --git a/test/unit/silent.test.js b/test/unit/silent.test.js new file mode 100644 index 000000000..961bb42ed --- /dev/null +++ b/test/unit/silent.test.js @@ -0,0 +1,35 @@ +describe('silent', () => { + it('should be suppressed translate warnings', done => { + const el = document.createElement('div') + const vm = new Vue({ + i18n: new VueI18n({ + locale: 'en', + silentTranslationWarn: true + }), + components: { + child1: { // translation with component + i18n: { + locale: 'en', + messages: { + en: { who: 'child1' }, + ja: { who: '子1' } + } + }, + render (h) { return h('div', {}, []) } + } + }, + render (h) { + return h('div', {}, [ + h('p', { ref: 'who' }, [this.$t('who')]), + h('child1', { ref: 'child1' }) + ]) + } + }).$mount(el) + + const child1 = vm.$refs.child1 + nextTick(() => { + vm.$t('foo.bar.buz') + child1.$t('foo.bar.buz') + }).then(done) + }) +})