Skip to content

Commit

Permalink
fix: cannot warn fallback root (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Mar 3, 2021
1 parent 71ab3a9 commit 738cf3e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
16 changes: 11 additions & 5 deletions packages/vue-i18n/src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
isObject
} from '@intlify/shared'
import {
isTranslateFallbackWarn,
isTranslateMissingWarn,
resolveValue,
createCoreContext,
MISSING_RESOLVE_VALUE,
Expand Down Expand Up @@ -232,9 +234,9 @@ export interface ComposerOptions<Message = VueMessageType> {
fallbackWarn?: boolean | RegExp
/**
* @remarks
* In the component localization, whether to fall back to root level (global) localization when localization fails.
* In the component localization, whether to fallback to root level (global scope) localization when localization fails.
*
* If `false`, it's warned, and is returned the key.
* If `false`, it's not fallback to root.
*
* @VueI18nSee [Fallbacking](../../guide/essentials/fallback)
*
Expand Down Expand Up @@ -411,7 +413,7 @@ export interface Composer<
fallbackWarn: boolean | RegExp
/**
* @remarks
* Whether to fall back to root level (global) localization when localization fails.
* Whether to fall back to root level (global scope) localization when localization fails.
*
* @VueI18nSee [Fallbacking](../../guide/essentials/fallback)
*/
Expand Down Expand Up @@ -1239,7 +1241,11 @@ export function createComposer<
if (isNumber(ret) && ret === NOT_REOSLVED) {
const key = argumentParser()
if (__DEV__ && __root) {
if (!_fallbackRoot) {
if (
_fallbackRoot &&
(isTranslateFallbackWarn(_fallbackWarn, key) ||
isTranslateMissingWarn(_missingWarn, key))
) {
warn(
getWarnMessage(I18nWarnCodes.FALLBACK_TO_ROOT, {
key,
Expand All @@ -1252,7 +1258,7 @@ export function createComposer<
const {
__emitter: emitter
} = (context as unknown) as CoreInternalContext
if (emitter) {
if (emitter && _fallbackRoot) {
emitter.emit(DevToolsTimelineEvents.FALBACK, {
type: warnType,
key,
Expand Down
4 changes: 2 additions & 2 deletions packages/vue-i18n/src/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ export interface VueI18nOptions {
missing?: MissingHandler
/**
* @remarks
* In the component localization, whether to fall back to root level (global) localization when localization fails.
* In the component localization, whether to fall back to root level (global scope) localization when localization fails.
*
* If `false`, it's warned, and is returned the key.
* If `false`, it's not fallback to root.
*
* @VueI18nSee [Fallbacking](../../guide/essentials/fallback)
*
Expand Down
23 changes: 17 additions & 6 deletions packages/vue-i18n/test/composer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
NumberPartsSymbol,
DatetimePartsSymbol
} from '../src/composer'
import { getWarnMessage, I18nWarnCodes } from '../src/warnings'
import { watch, watchEffect, nextTick, Text, createVNode } from 'vue'
import {
Locale,
Expand Down Expand Up @@ -409,19 +410,21 @@ describe('fallbackRoot', () => {
expect(composer.fallbackRoot).toEqual(true)
})

test('not warnings', () => {
test('warnings', () => {
const mockWarn = warn as jest.MockedFunction<typeof warn>
mockWarn.mockImplementation(() => {})

const root = createComposer({
locale: 'en',
missingWarn: false,
fallbackWarn: true,
fallbackRoot: true
})
const { t } = createComposer({
locale: 'en',
fallbackLocale: ['fr', 'jp'],
missingWarn: false,
fallbackWarn: true,
fallbackRoot: true,
messages: {
ja: {},
Expand All @@ -431,23 +434,31 @@ describe('fallbackRoot', () => {
__root: root
} as any)
expect(t('hello')).toEqual('hello')
expect(mockWarn).not.toHaveBeenCalled()
expect(mockWarn).toHaveBeenCalled()
expect(mockWarn.mock.calls[0][0]).toEqual(
getWarnMessage(I18nWarnCodes.FALLBACK_TO_ROOT, {
type: 'translate',
key: 'hello'
})
)
})

test('warnings', () => {
test('not warnings', () => {
const mockWarn = warn as jest.MockedFunction<typeof warn>
mockWarn.mockImplementation(() => {})

const root = createComposer({
locale: 'en',
missingWarn: false,
fallbackRoot: false
fallbackWarn: false,
fallbackRoot: true
})
const { t } = createComposer({
locale: 'en',
fallbackLocale: ['fr', 'jp'],
missingWarn: false,
fallbackRoot: false,
fallbackWarn: false,
fallbackRoot: true,
messages: {
ja: {},
en: {},
Expand All @@ -456,7 +467,7 @@ describe('fallbackRoot', () => {
__root: root
} as any)
expect(t('hello')).toEqual('hello')
expect(mockWarn).toHaveBeenCalledTimes(1)
expect(mockWarn).not.toHaveBeenCalledTimes(1)
})
})

Expand Down

0 comments on commit 738cf3e

Please sign in to comment.