Skip to content

Commit b45fd06

Browse files
authored
fix: fallback root warnings (#290)
closes #281
1 parent 58cd811 commit b45fd06

File tree

2 files changed

+80
-21
lines changed

2 files changed

+80
-21
lines changed

packages/vue-i18n/src/composer.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -207,24 +207,26 @@ export interface ComposerOptions<Message = VueMessageType> {
207207
* @remarks
208208
* Whether suppress warnings outputted when localization fails.
209209
*
210-
* If `true`, suppress localization fail warnings.
210+
* If `false`, suppress localization fail warnings.
211211
*
212212
* If you use regular expression, you can suppress localization fail warnings that it match with translation key (e.g. `t`).
213213
*
214214
* @VueI18nSee [Fallbacking](../../guide/essentials/fallback)
215215
*
216-
* @defaultValue `false`
216+
* @defaultValue `true`
217217
*/
218218
missingWarn?: boolean | RegExp
219219
/**
220220
* @remarks
221-
* Whether do template interpolation on translation keys when your language lacks a translation for a key.
221+
* Whether suppress warnings when falling back to either `fallbackLocale` or root.
222222
*
223-
* If `true`, skip writing templates for your "base" language; the keys are your templates.
223+
* If `false`, suppress fall back warnings.
224+
*
225+
* If you use regular expression, you can suppress fallback warnings that it match with translation key (e.g. `t`).
224226
*
225227
* @VueI18nSee [Fallbacking](../../guide/essentials/fallback)
226228
*
227-
* @defaultValue `false`
229+
* @defaultValue `true`
228230
*/
229231
fallbackWarn?: boolean | RegExp
230232
/**
@@ -240,7 +242,9 @@ export interface ComposerOptions<Message = VueMessageType> {
240242
fallbackRoot?: boolean
241243
/**
242244
* @remarks
243-
* Whether suppress warnings when falling back to either `fallbackLocale` or root.
245+
* Whether do template interpolation on translation keys when your language lacks a translation for a key.
246+
*
247+
* If `true`, skip writing templates for your "base" language; the keys are your templates.
244248
*
245249
* @VueI18nSee [Fallbacking](../../guide/essentials/fallback)
246250
*
@@ -399,14 +403,14 @@ export interface Composer<
399403
missingWarn: boolean | RegExp
400404
/**
401405
* @remarks
402-
* Whether suppress fallback warnings when localization fails.
406+
* Whether suppress fall back warnings when localization fails.
407+
*
408+
* @VueI18nSee [Fallbacking](../../guide/essentials/fallback)
403409
*/
404410
fallbackWarn: boolean | RegExp
405411
/**
406412
* @remarks
407-
* Whether to fallback to root level (global) localization when localization fails.
408-
*
409-
* If `false`, it's warned, and is returned the key.
413+
* Whether to fall back to root level (global) localization when localization fails.
410414
*
411415
* @VueI18nSee [Fallbacking](../../guide/essentials/fallback)
412416
*/
@@ -1071,9 +1075,12 @@ export function createComposer<
10711075
? options.fallbackWarn
10721076
: true
10731077

1074-
let _fallbackRoot = isBoolean(options.fallbackRoot)
1075-
? options.fallbackRoot
1076-
: true
1078+
// prettier-ignore
1079+
let _fallbackRoot = __root
1080+
? __root.fallbackRoot
1081+
: isBoolean(options.fallbackRoot)
1082+
? options.fallbackRoot
1083+
: true
10771084

10781085
// configure fall bakck to root
10791086
let _fallbackFormat = !!options.fallbackFormat
@@ -1221,13 +1228,15 @@ export function createComposer<
12211228
const ret = fn(context) // track reactive dependency, see the getRuntimeContext
12221229
if (isNumber(ret) && ret === NOT_REOSLVED) {
12231230
const key = argumentParser()
1224-
if (__DEV__ && _fallbackRoot && __root) {
1225-
warn(
1226-
getWarnMessage(I18nWarnCodes.FALLBACK_TO_ROOT, {
1227-
key,
1228-
type: warnType
1229-
})
1230-
)
1231+
if (__DEV__ && __root) {
1232+
if (!_fallbackRoot) {
1233+
warn(
1234+
getWarnMessage(I18nWarnCodes.FALLBACK_TO_ROOT, {
1235+
key,
1236+
type: warnType
1237+
})
1238+
)
1239+
}
12311240
// for vue-devtools timeline event
12321241
if (__DEV__) {
12331242
const {
@@ -1243,7 +1252,7 @@ export function createComposer<
12431252
}
12441253
}
12451254
}
1246-
return _fallbackRoot && __root
1255+
return __root && _fallbackRoot
12471256
? fallbackSuccess((__root as unknown) as Composer<T> & ComposerInternal)
12481257
: fallbackFail(key)
12491258
} else if (successCondition(ret)) {

packages/vue-i18n/test/composer.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,56 @@ describe('fallbackRoot', () => {
400400
composer.fallbackRoot = true
401401
expect(composer.fallbackRoot).toEqual(true)
402402
})
403+
404+
test('not warnings', () => {
405+
const mockWarn = warn as jest.MockedFunction<typeof warn>
406+
mockWarn.mockImplementation(() => {})
407+
408+
const root = createComposer({
409+
locale: 'en',
410+
missingWarn: false,
411+
fallbackRoot: true
412+
})
413+
const { t } = createComposer({
414+
locale: 'en',
415+
fallbackLocale: ['fr', 'jp'],
416+
missingWarn: false,
417+
fallbackRoot: true,
418+
messages: {
419+
ja: {},
420+
en: {},
421+
fr: {}
422+
},
423+
__root: root
424+
} as any)
425+
expect(t('hello')).toEqual('hello')
426+
expect(mockWarn).not.toHaveBeenCalled()
427+
})
428+
429+
test('warnings', () => {
430+
const mockWarn = warn as jest.MockedFunction<typeof warn>
431+
mockWarn.mockImplementation(() => {})
432+
433+
const root = createComposer({
434+
locale: 'en',
435+
missingWarn: false,
436+
fallbackRoot: false
437+
})
438+
const { t } = createComposer({
439+
locale: 'en',
440+
fallbackLocale: ['fr', 'jp'],
441+
missingWarn: false,
442+
fallbackRoot: false,
443+
messages: {
444+
ja: {},
445+
en: {},
446+
fr: {}
447+
},
448+
__root: root
449+
} as any)
450+
expect(t('hello')).toEqual('hello')
451+
expect(mockWarn).toHaveBeenCalledTimes(1)
452+
})
403453
})
404454

405455
describe('warnHtmlMessage', () => {

0 commit comments

Comments
 (0)