From 46de19e1182891ceba1985b32b4ab9f557783bdf Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Fri, 9 Aug 2019 19:25:38 +0900 Subject: [PATCH] :star: new: datetime/number formats fallback warning filter closes #558 --- src/index.js | 16 +++++++-------- test/unit/silent.test.js | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index 7c5b247c2..12abb2d40 100644 --- a/src/index.js +++ b/src/index.js @@ -621,8 +621,8 @@ export default class VueI18n { // fallback locale if (isNull(formats) || isNull(formats[key])) { - if (process.env.NODE_ENV !== 'production' && !this._isSilentTranslationWarn(key)) { - warn(`Fall back to '${fallback}' datetime formats from '${locale} datetime formats.`) + if (process.env.NODE_ENV !== 'production' && !this._isSilentTranslationWarn(key) && !this._isSilentFallbackWarn(key)) { + warn(`Fall back to '${fallback}' datetime formats from '${locale}' datetime formats.`) } _locale = fallback formats = dateTimeFormats[_locale] @@ -655,8 +655,8 @@ export default class VueI18n { const ret: ?DateTimeFormatResult = this._localizeDateTime(value, locale, this.fallbackLocale, this._getDateTimeFormats(), key) if (this._isFallbackRoot(ret)) { - if (process.env.NODE_ENV !== 'production' && !this._isSilentTranslationWarn(key)) { - warn(`Fall back to datetime localization of root: key '${key}' .`) + if (process.env.NODE_ENV !== 'production' && !this._isSilentTranslationWarn(key) && !this._isSilentFallbackWarn(key)) { + warn(`Fall back to datetime localization of root: key '${key}'.`) } /* istanbul ignore if */ if (!this._root) { throw Error('unexpected error') } @@ -718,8 +718,8 @@ export default class VueI18n { // fallback locale if (isNull(formats) || isNull(formats[key])) { - if (process.env.NODE_ENV !== 'production' && !this._isSilentTranslationWarn(key)) { - warn(`Fall back to '${fallback}' number formats from '${locale} number formats.`) + if (process.env.NODE_ENV !== 'production' && !this._isSilentTranslationWarn(key) && !this._isSilentFallbackWarn(key)) { + warn(`Fall back to '${fallback}' number formats from '${locale}' number formats.`) } _locale = fallback formats = numberFormats[_locale] @@ -762,8 +762,8 @@ export default class VueI18n { const formatter: ?Object = this._getNumberFormatter(value, locale, this.fallbackLocale, this._getNumberFormats(), key, options) const ret: ?NumberFormatResult = formatter && formatter.format(value) if (this._isFallbackRoot(ret)) { - if (process.env.NODE_ENV !== 'production' && !this._isSilentTranslationWarn(key)) { - warn(`Fall back to number localization of root: key '${key}' .`) + if (process.env.NODE_ENV !== 'production' && !this._isSilentTranslationWarn(key) && !this._isSilentFallbackWarn(key)) { + warn(`Fall back to number localization of root: key '${key}'.`) } /* istanbul ignore if */ if (!this._root) { throw Error('unexpected error') } diff --git a/test/unit/silent.test.js b/test/unit/silent.test.js index bf2f281a8..8f1bbb639 100644 --- a/test/unit/silent.test.js +++ b/test/unit/silent.test.js @@ -1,3 +1,6 @@ +import dateTimeFormats from './fixture/datetime' +import numberFormats from './fixture/number' + describe('silent', () => { let spy beforeEach(() => { @@ -222,5 +225,44 @@ describe('silent', () => { assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === false) }) }) + + describe('datetime/number format', () => { + it('should be suppressed translate warnings', () => { + const el = document.createElement('div') + const root = new Vue({ + i18n: new VueI18n({ + locale: 'en-US', + fallbackLocale: 'ja-JP', + silentFallbackWarn: true, + dateTimeFormats, + numberFormats + }), + components: { + subComponent: { + i18n: { + dateTimeFormats: {}, + numberFormats: {} + }, + render (h) { return h('p') } + } + }, + render (h) { return h('sub-component') } + }).$mount(el) + const vm = root.$children[0] + const warningDateTimeRegex = /Fall back to .* datetime formats\./ + const warningNumberRegex = /Fall back to .* number formats\./ + vm.$d(Date.now(), 'long') + vm.$n(10, 'numeric') + assert(spy.getCalls().some(call => call.args[0].match(warningDateTimeRegex)) === false) + assert(spy.getCalls().some(call => call.args[0].match(warningNumberRegex)) === false) + + // change + vm.$i18n.silentFallbackWarn = false + vm.$d(Date.now(), 'long') + vm.$n(10, 'numeric') + assert(spy.getCalls().some(call => call.args[0].match(warningDateTimeRegex)) === true) + assert(spy.getCalls().some(call => call.args[0].match(warningNumberRegex)) === true) + }) + }) }) })