Update UI and improve i18n loading logic

Major changes include enhancements to the UI and modifications to the i18n loading logic to more effectively handle namespaces. Several components were updated to improve readability and layout consistency. The i18n loading logic now includes additional handling for waiting until all namespaces are loaded before the i18n instance is returned, with a warning if it takes longer than expected. Furthermore, code have been refactored for fonts, buttons, and other UI elements.
This commit is contained in:
gbuomprisco
2024-07-18 04:01:45 +08:00
parent 35717c79f7
commit 342c96abfa
16 changed files with 155 additions and 141 deletions

View File

@@ -13,12 +13,14 @@ export async function initializeServerI18n(
resolver: (language: string, namespace: string) => Promise<object>,
) {
const i18nInstance = createInstance();
const loadedNamespaces = new Set<string>();
await i18nInstance
.use(
resourcesToBackend(async (language, namespace, callback) => {
try {
const data = await resolver(language, namespace);
loadedNamespaces.add(namespace);
return callback(null, data);
} catch (error) {
@@ -27,16 +29,50 @@ export async function initializeServerI18n(
error,
);
return {};
return callback(null, {});
}
}),
)
.use(initReactI18next)
.init(settings, (error) => {
if (error) {
console.error('Error initializing i18n server', error);
.init(settings);
const namespaces = settings.ns as string[];
// If all namespaces are already loaded, return the i18n instance
if (loadedNamespaces.size === namespaces.length) {
return i18nInstance;
}
// Otherwise, wait for all namespaces to be loaded
const maxWaitTime = 0.1; // 100 milliseconds
const checkIntervalMs = 5; // 5 milliseconds
async function waitForNamespaces() {
const startTime = Date.now();
while (Date.now() - startTime < maxWaitTime) {
const allNamespacesLoaded = namespaces.every((ns) =>
loadedNamespaces.has(ns),
);
if (allNamespacesLoaded) {
return true;
}
});
await new Promise((resolve) => setTimeout(resolve, checkIntervalMs));
}
return false;
}
const success = await waitForNamespaces();
if (!success) {
console.warn(
`Not all namespaces were loaded after ${maxWaitTime}ms. Initialization may be incomplete.`,
);
}
return i18nInstance;
}