Skip to content
Closed
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
26 changes: 26 additions & 0 deletions packages/core/src/i18n/i18n.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@ describe('message registry', () => {
).toBe('21 fallback items')
})

test('uses the fallback catalog locale plural rules when falling back from pt-PT to pt', async () => {
// In CLDR, `pt` selects "one" for 0 while `pt-PT` selects "other" for 0.
await registerMessages('pt', 'viewer', {
'test.pt-fallback-plural': '{count, plural, one {# item} other {# itens}}',
})
await registerMessages('pt-PT', 'viewer', {
'test.pt-pt-override-plural': '{count, plural, one {# artigo} other {# artigos}}',
})
setLocale('pt-PT')

// Message came from `pt`: must use `pt`'s plural rules (0 -> "one").
expect(
translate('viewer.test.pt-fallback-plural', '{count, plural, one {# item} other {# items}}', {
count: 0,
}),
).toBe('0 item')
// Message came from `pt-PT`: must use `pt-PT`'s plural rules (0 -> "other").
expect(
translate(
'viewer.test.pt-pt-override-plural',
'{count, plural, one {# article} other {# articles}}',
{ count: 0 },
),
).toBe('0 artigos')
})

test('notifies subscribers for locale and active-catalog changes', async () => {
let changes = 0
let localeChanges = 0
Expand Down
44 changes: 41 additions & 3 deletions packages/core/src/i18n/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ function warnMissing(key: string, requestedLocale: string): void {
export class PascalI18n {
readonly i18n: LinguiI18n
private readonly englishI18n: LinguiI18n
private readonly runtimePool = new Map<string, LinguiI18n>()
private fallbackRuntimes: Array<{ locale: string; i18n: LinguiI18n }> = []
private readonly listeners = new Set<() => void>()
private readonly localeListeners = new Set<(locale: string) => void>()
private retentionCount = 0
Expand Down Expand Up @@ -173,15 +175,29 @@ export class PascalI18n {
scopes.delete(this)
this.listeners.clear()
this.localeListeners.clear()
this.runtimePool.clear()
this.fallbackRuntimes = []
}

_(descriptor: MessageDescriptor): string
_(id: string, values?: Values, options?: MessageOptions): string
_(idOrDescriptor: string | MessageDescriptor, values?: Values, options?: MessageOptions): string {
const id = typeof idOrDescriptor === 'string' ? idOrDescriptor : idOrDescriptor.id
const translated = this.locale === 'en' || Object.hasOwn(this.i18n.messages, id)
const runtime = translated ? this.i18n : this.englishI18n
if (!translated) warnMissing(id, this.locale)
let runtime: LinguiI18n | undefined
if (this.locale === 'en') {
runtime = this.englishI18n
} else {
for (const candidate of this.fallbackRuntimes) {
if (Object.hasOwn(candidate.i18n.messages, id)) {
runtime = candidate.i18n
break
}
}
}
if (!runtime) {
warnMissing(id, this.locale)
runtime = this.englishI18n
}
return typeof idOrDescriptor === 'string'
? runtime._(idOrDescriptor, values, options)
: runtime._(idOrDescriptor)
Expand All @@ -196,6 +212,28 @@ export class PascalI18n {
locale: 'en',
messages: mergedMessages('en'),
})
const runtimes: Array<{ locale: string; i18n: LinguiI18n }> = []
if (this.locale !== 'en') {
for (const candidate of localeFallbacks(this.locale)) {
if (candidate === 'en') {
runtimes.push({ locale: 'en', i18n: this.englishI18n })
continue
}
const catalog = catalogs.get(candidate)
if (!catalog) continue
let candidateI18n = this.runtimePool.get(candidate)
if (!candidateI18n) {
candidateI18n = setupI18n()
this.runtimePool.set(candidate, candidateI18n)
}
candidateI18n.loadAndActivate({
locale: candidate,
messages: Object.assign(emptyMessages(), catalog),
})
runtimes.push({ locale: candidate, i18n: candidateI18n })
}
}
this.fallbackRuntimes = runtimes
if (!notify) return
this.version += 1
for (const listener of [...this.listeners]) listener()
Expand Down