Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/core-base/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,32 @@ 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(
targetLocale: Locale,
locales: Locale[]
): boolean {
const index = locales.indexOf(targetLocale)
if (index === -1) {
return false
}

for (let i = index + 1; i < locales.length; i++) {
if (isAlmostSameLocale(targetLocale, locales[i])) {
return true
}
}

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, locales)) {
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
61 changes: 61 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,67 @@ test('issue #1738', async () => {
expect(wrapper.find('#te2')?.textContent).toEqual(`true - expected true`)
})

describe('issue #1768', () => {
test('Implicit fallback using locales', 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('Explicit fallback with decision maps', async () => {
const mockWarn = vi.spyOn(shared, 'warn')
// eslint-disable-next-line @typescript-eslint/no-empty-function
mockWarn.mockImplementation(() => {})

const i18n = createI18n({
locale: 'zh-Hant',
fallbackLocale: {
'de-CH': ['fr', 'it'],
'zh-Hant': ['zh-Hans'],
'es-CL': ['es-AR'],
es: ['en-GB'],
pt: ['es-AR'],
default: ['en', 'da']
},
messages: {
zh: {
hello: {
'vue-i18n': '你好,Vue I18n'
}
}
}
})

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

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

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