Skip to content

Commit

Permalink
⭐ new: datetime/number formats fallback warning filter
Browse files Browse the repository at this point in the history
closes #558
  • Loading branch information
kazupon committed Aug 9, 2019
1 parent caa90fa commit 46de19e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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') }
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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') }
Expand Down
42 changes: 42 additions & 0 deletions test/unit/silent.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import dateTimeFormats from './fixture/datetime'
import numberFormats from './fixture/number'

describe('silent', () => {
let spy
beforeEach(() => {
Expand Down Expand Up @@ -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)
})
})
})
})

0 comments on commit 46de19e

Please sign in to comment.