Skip to content

Commit

Permalink
Fix i18n (#6222)
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
  • Loading branch information
BykhovDenis authored Aug 1, 2024
1 parent e124978 commit 0d18674
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions packages/platform/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export type Loader = (locale: string) => Promise<Record<string, string | Record<
type Messages = Record<string, IntlString | Record<string, IntlString>>

const loaders = new Map<Plugin, Loader>()
const translations = new Map<Plugin, Messages | Status>()
const cache = new Map<IntlString, IntlMessageFormat | Status>()
const translations = new Map<string, Map<Plugin, Messages | Status>>()
const cache = new Map<string, Map<IntlString, IntlMessageFormat | Status>>()
const englishTranslationsForMissing = new Map<Plugin, Messages | Status>()
/**
* @public
Expand All @@ -52,10 +52,14 @@ export async function loadPluginStrings (locale: string, force: boolean = false)
cache.clear()
}
for (const [plugin] of loaders) {
let messages = translations.get(plugin)
const localtTanslations = translations.get(locale) ?? new Map<Plugin, Messages | Status<any>>()
if (!translations.has(locale)) {
translations.set(locale, localtTanslations)
}
let messages = localtTanslations.get(plugin)
if (messages === undefined || force) {
messages = await loadTranslationsForComponent(plugin, locale)
translations.set(plugin, messages)
localtTanslations.set(plugin, messages)
}
}
}
Expand Down Expand Up @@ -83,10 +87,14 @@ async function loadTranslationsForComponent (plugin: Plugin, locale: string): Pr

async function getTranslation (id: _IdInfo, locale: string): Promise<IntlString | Status | undefined> {
try {
let messages = translations.get(id.component)
const localtTanslations = translations.get(locale) ?? new Map<Plugin, Messages | Status<any>>()
if (!translations.has(locale)) {
translations.set(locale, localtTanslations)
}
let messages = localtTanslations.get(id.component)
if (messages === undefined) {
messages = await loadTranslationsForComponent(id.component, locale)
translations.set(id.component, messages)
localtTanslations.set(id.component, messages)
}
if (messages instanceof Status) {
return messages
Expand Down Expand Up @@ -127,7 +135,11 @@ export async function translate<P extends Record<string, any>> (
language?: string
): Promise<string> {
const locale = language ?? getMetadata(platform.metadata.locale) ?? 'en'
const compiled = cache.get(message)
const localCache = cache.get(locale) ?? new Map<IntlString, IntlMessageFormat | Status>()
if (!cache.has(locale)) {
cache.set(locale, localCache)
}
const compiled = localCache.get(message)

if (compiled !== undefined) {
if (compiled instanceof Status) {
Expand All @@ -142,16 +154,16 @@ export async function translate<P extends Record<string, any>> (
}
const translation = (await getTranslation(id, locale)) ?? message
if (translation instanceof Status) {
cache.set(message, translation)
localCache.set(message, translation)
return message
}
const compiled = new IntlMessageFormat(translation, locale, undefined, { ignoreTag: true })
cache.set(message, compiled)
localCache.set(message, compiled)
return compiled.format(params)
} catch (err) {
const status = unknownError(err)
await setPlatformStatus(status)
cache.set(message, status)
localCache.set(message, status)
return message
}
}
Expand Down

0 comments on commit 0d18674

Please sign in to comment.