Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: not throw warnings when using implicit fallback #1798

Merged
merged 13 commits into from
Apr 14, 2024
Merged
31 changes: 31 additions & 0 deletions packages/core-base/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,35 @@ export function updateFallbackLocale<Message = string>(
ctx.localeFallbacker<Message>(ctx, fallback, locale)
}

/** @internal */
export function isAlmostSameLocale(
locale: Locale,
compareLocale: Locale
): boolean {
if (locale === compareLocale) return false
return locale.split('-')[0] === compareLocale.split('-')[0]
}

/** @internal */
export function isImplicitFallback(
locale: Locale,
fallbackLocale: FallbackLocale
): boolean {
if (isString(fallbackLocale)) {
return isAlmostSameLocale(locale, fallbackLocale)
}

if (isArray(fallbackLocale)) {
return fallbackLocale.some(fbLocale => isAlmostSameLocale(locale, fbLocale))
}

if (isObject(fallbackLocale)) {
return Object.values(fallbackLocale).some(fbLocales => {
return fbLocales.some(fbLocale => isAlmostSameLocale(locale, fbLocale))
})
}

return false
}

/* eslint-enable @typescript-eslint/no-explicit-any */
23 changes: 14 additions & 9 deletions packages/core-base/src/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { isMessageAST } from './compilation'
import { createMessageContext } from './runtime'
import {
isTranslateFallbackWarn,
isAlmostSameLocale,
isImplicitFallback,
handleMissing,
NOT_REOSLVED,
getAdditionalMeta,
Expand Down Expand Up @@ -839,6 +841,7 @@ function resolveMessageFormat<Messages, Message>(
if (
__DEV__ &&
locale !== targetLocale &&
!isAlmostSameLocale(locale, targetLocale) &&
isTranslateFallbackWarn(fallbackWarn, key)
) {
onWarn(
Expand Down Expand Up @@ -905,15 +908,17 @@ function resolveMessageFormat<Messages, Message>(
break
}

const missingRet = handleMissing(
context as any, // eslint-disable-line @typescript-eslint/no-explicit-any
key,
targetLocale,
missingWarn,
type
)
if (missingRet !== key) {
format = missingRet as PathValue
if (!isImplicitFallback(targetLocale, fallbackLocale)) {
const missingRet = handleMissing(
context as any, // eslint-disable-line @typescript-eslint/no-explicit-any
key,
targetLocale,
missingWarn,
type
)
if (missingRet !== key) {
format = missingRet as PathValue
}
}
from = to
}
Expand Down
26 changes: 26 additions & 0 deletions packages/vue-i18n-core/test/issues.test.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also like to see additional tests for complex fallbacks using decision maps. 🙏
https://vue-i18n.intlify.dev/guide/essentials/fallback.html#explicit-fallback-with-decision-maps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review!
I have added tests for complex fallbacks using decision maps, so I hope you'll check it out 🙏

Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,32 @@ test('issue #1738', async () => {
expect(wrapper.find('#te2')?.textContent).toEqual(`true - expected true`)
})

test('issue #1768', async () => {
const mockWarn = vi.spyOn(shared, 'warn')
// eslint-disable-next-line @typescript-eslint/no-empty-function
mockWarn.mockImplementation(() => {})

const i18n = createI18n({
locale: 'en-US',
fallbackLocale: 'en',
messages: {
en: {
hello: {
'vue-i18n': 'Hello, Vue I18n'
}
}
}
})

const App = defineComponent({
template: `<div>{{ $t('hello.vue-i18n') }}</div>`
})
const wrapper = await mount(App, i18n)

expect(wrapper.html()).toEqual('<div>Hello, Vue I18n</div>')
expect(mockWarn).toHaveBeenCalledTimes(0)
})

test('#1796', async () => {
const i18n = createI18n({
locale: 'en',
Expand Down
Loading