-
-
Notifications
You must be signed in to change notification settings - Fork 861
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ improvement(index): silence fallback warnings (#510) by @SzNagyMisu
* ⚡ improvement(option): silentFallbackWarn (#139) * feature(option): add silentFallbackWarn to VueI18n constructor * silence fallback warnings * warn only if no translation is found at all * adding typescript property declaration * 📝 docs(options): document silentFallbackWarn * Update vuepress/api/README.md Co-Authored-By: SzNagyMisu <[email protected]> * ⚡ improvement(option): silentFallbackWarn * include case when pathRet is not null, undefined, array, plain object or string * provide test case
- Loading branch information
1 parent
e879024
commit ddc0c79
Showing
6 changed files
with
203 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,168 @@ | ||
describe('silent', () => { | ||
it('should be suppressed translate warnings', () => { | ||
const vm = new Vue({ | ||
i18n: new VueI18n({ | ||
locale: 'en', | ||
silentTranslationWarn: true, | ||
let spy | ||
beforeEach(() => { | ||
spy = sinon.spy(console, 'warn') | ||
}) | ||
afterEach(() => { | ||
spy.restore() | ||
}) | ||
|
||
describe('silentTranslationWarn', () => { | ||
it('should be suppressed translate warnings', () => { | ||
const vm = new Vue({ | ||
i18n: new VueI18n({ | ||
locale: 'en', | ||
silentTranslationWarn: true, | ||
messages: { | ||
en: { who: 'root' }, | ||
ja: { who: 'ルート' } | ||
} | ||
}) | ||
}) | ||
|
||
vm.$t('foo.bar.buz') | ||
assert(spy.notCalled === true) | ||
|
||
// change | ||
vm.$i18n.silentTranslationWarn = false | ||
vm.$t('foo.bar.buz') | ||
assert(spy.callCount === 2) | ||
}) | ||
}) | ||
|
||
describe('silentFallbackWarn', () => { | ||
let i18n | ||
beforeEach(() => { | ||
i18n = new VueI18n({ | ||
locale: 'hu', | ||
fallbackLocale: 'en', | ||
silentFallbackWarn: true, | ||
messages: { | ||
en: { who: 'root' }, | ||
ja: { who: 'ルート' } | ||
en: { winner: 'winner' }, | ||
hu: { chickenDinner: 'csirkevacsora' } | ||
} | ||
}) | ||
}) | ||
|
||
const spy = sinon.spy(console, 'warn') | ||
vm.$t('foo.bar.buz') | ||
assert(spy.notCalled === true) | ||
it('should suppress `Fall back to ${fallback} locale` warnings', () => { | ||
const vm = new Vue({ i18n }) | ||
const warningRegex = /Fall back to .* 'en' locale./ | ||
vm.$t('winner') | ||
assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === false) | ||
|
||
// change | ||
vm.$i18n.silentTranslationWarn = false | ||
vm.$t('foo.bar.buz') | ||
assert(spy.callCount === 2) | ||
vm.$i18n.silentFallbackWarn = false | ||
vm.$t('winner') | ||
assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) | ||
}) | ||
|
||
spy.restore() | ||
it('should suppress `Fall back to root locale` warnings.', () => { | ||
const el = document.createElement('div') | ||
const root = new Vue({ | ||
i18n, | ||
components: { | ||
subComponent: { | ||
i18n: { messages: { hu: { name: 'Név' } } }, | ||
render (h) { return h('p') } | ||
} | ||
}, | ||
render (h) { return h('sub-component') } | ||
}).$mount(el) | ||
const vm = root.$children[0] | ||
const warningRegex = /Fall back to .* root locale./ | ||
|
||
vm.$t('chickenDinner') | ||
assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === false) | ||
|
||
vm.$i18n.silentFallbackWarn = false | ||
vm.$t('chickenDinner') | ||
assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) | ||
}) | ||
|
||
describe('if first try is null or undefined,', () => { | ||
it('should suppress `not a string` warnings for fallback to fallbackLocale.', () => { | ||
const vm = new Vue({ i18n }) | ||
const warningRegex = /Value of .* is not a string./ | ||
vm.$t('winner') | ||
assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === false) | ||
|
||
vm.$i18n.silentFallbackWarn = false | ||
vm.$t('winner') | ||
assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) | ||
}) | ||
|
||
it('should supress `not a string` warnings for fallback to root.', () => { | ||
const el = document.createElement('div') | ||
const root = new Vue({ | ||
i18n, | ||
components: { | ||
subComponent: { | ||
i18n: { messages: { hu: { name: 'Név' } } }, | ||
render (h) { return h('p') } | ||
} | ||
}, | ||
render (h) { return h('sub-component') } | ||
}).$mount(el) | ||
const vm = root.$children[0] | ||
const warningRegex = /Value of .* is not a string./ | ||
vm.$t('chickenDinner') | ||
assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === false) | ||
|
||
vm.$i18n.silentFallbackWarn = false | ||
vm.$t('chickenDinner') | ||
assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) | ||
}) | ||
}) | ||
|
||
describe('if first try is not null, undefined, array, plain object or string,', () => { | ||
it('should suppress `not a string` warnings for fallback to fallbackLocale.', () => { | ||
const vm = new Vue({ | ||
i18n: new VueI18n({ | ||
locale: 'hu', | ||
fallbackLocale: 'en', | ||
silentFallbackWarn: true, | ||
messages: { | ||
en: { winner: 'winner' }, | ||
hu: { winner: true } // translation value is boolean | ||
} | ||
}) | ||
}) | ||
const warningRegex = /Value of .* is not a string./ | ||
vm.$t('winner') | ||
assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === false) | ||
|
||
vm.$i18n.silentFallbackWarn = false | ||
vm.$t('winner') | ||
assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) | ||
}) | ||
|
||
it('should supress `not a string` warnings for fallback to root.', () => { | ||
const el = document.createElement('div') | ||
const root = new Vue({ | ||
i18n, | ||
components: { | ||
subComponent: { | ||
i18n: { messages: { hu: { chickenDinner: 11 } } }, // translation value is number | ||
render (h) { return h('p') } | ||
} | ||
}, | ||
render (h) { return h('sub-component') } | ||
}).$mount(el) | ||
const vm = root.$children[0] | ||
const warningRegex = /Value of .* is not a string./ | ||
vm.$t('chickenDinner') | ||
assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === false) | ||
|
||
vm.$i18n.silentFallbackWarn = false | ||
vm.$t('chickenDinner') | ||
assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) | ||
}) | ||
}) | ||
|
||
it('should not suppress `not a string` warnings when no further fallback is possible.', () => { | ||
const vm = new Vue({ i18n }) | ||
const warningRegex = /Value of .* is not a string./ | ||
vm.$t('loser') | ||
assert(spy.getCalls().some(call => call.args[0].match(warningRegex)) === true) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters