Refactor i18n client and server initialization

The refactor includes the removal of clientInstance checks as they are unnecessary. Also, i18n-provider.tsx has been updated to use react-query instead of local state for client initialization. Lastly, error handling has been added to the server initialization process and the @tanstack/react-query package has been added to the project dependencies.
This commit is contained in:
giancarlo
2024-04-21 12:48:18 +08:00
parent 562db0058e
commit c5b70d7b62
5 changed files with 39 additions and 30 deletions

View File

@@ -3,8 +3,6 @@ import LanguageDetector from 'i18next-browser-languagedetector';
import resourcesToBackend from 'i18next-resources-to-backend';
import { initReactI18next } from 'react-i18next';
let clientInstance: i18n | null = null;
/**
* Initialize the i18n instance on the client.
* @param settings - the i18n settings
@@ -14,10 +12,6 @@ export async function initializeI18nClient(
settings: InitOptions,
resolver: (lang: string, namespace: string) => Promise<object>,
): Promise<i18n> {
if (clientInstance) {
return Promise.resolve(clientInstance);
}
await i18next
.use(
resourcesToBackend(async (language, namespace, callback) => {
@@ -47,7 +41,5 @@ export async function initializeI18nClient(
},
);
clientInstance = i18next;
return clientInstance;
return i18next;
}