From 08fc8e6d017462f79bb5a3a2bec21720f636081d Mon Sep 17 00:00:00 2001 From: giancarlo Date: Sat, 20 Apr 2024 00:46:45 +0800 Subject: [PATCH] Improve i18n resource loading mechanism Updated i18n client to track not only loaded languages but also namespaces. The component will now remain suspended until all languages and namespaces are loaded. This improvement ensures that all necessary localisation resources are fully ready before rendering components. --- packages/i18n/src/i18n.client.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/i18n/src/i18n.client.ts b/packages/i18n/src/i18n.client.ts index ebc7373ee..7ee7ed98c 100644 --- a/packages/i18n/src/i18n.client.ts +++ b/packages/i18n/src/i18n.client.ts @@ -19,13 +19,20 @@ export async function initializeI18nClient( } const loadedLanguages: string[] = []; + const loadedNamespaces: string[] = []; await i18next .use( resourcesToBackend(async (language, namespace, callback) => { const data = await resolver(language, namespace); - loadedLanguages.push(language); + if (!loadedLanguages.includes(language)) { + loadedLanguages.push(language); + } + + if (!loadedNamespaces.includes(namespace)) { + loadedNamespaces.push(namespace); + } return callback(null, data); }), @@ -51,8 +58,16 @@ export async function initializeI18nClient( }, ); - // keep component suspended until all languages are loaded - if (loadedLanguages.length !== settings.ns?.length) { + // keep component suspended until all languages and namespaces are loaded + + if (loadedNamespaces.length !== settings.ns?.length) { + throw new Error(); + } + + if ( + loadedLanguages.length !== + ((settings.supportedLngs as string[]) ?? [])?.length + ) { throw new Error(); }