refactor(i18n): optimize translation loading and initialization

- Updated ensureTranslationsLoaded function to prevent duplicate requests by caching loaded translations and managing loading promises.
- Simplified translation preloading on app startup to only load essential namespaces for the current language.
- Adjusted useTranslationPreloader hook to avoid multiple requests for translations and ensure efficient loading state management.
This commit is contained in:
chamikaJ
2025-07-07 12:31:11 +05:30
parent a6f9046b42
commit 26b47aac53
3 changed files with 81 additions and 32 deletions

View File

@@ -26,7 +26,7 @@ export const useTranslationPreloader = (
try {
setIsLoading(true);
// Ensure translations are loaded
// Only load translations for current language to avoid multiple requests
await ensureTranslationsLoaded(namespaces);
// Wait for i18next to be ready
@@ -47,12 +47,18 @@ export const useTranslationPreloader = (
}
};
loadTranslations();
// Only load if not already loaded
if (!isLoaded && !ready) {
loadTranslations();
} else if (ready && !isLoaded) {
setIsLoaded(true);
setIsLoading(false);
}
return () => {
isMounted = false;
};
}, [namespaces, ready]);
}, [namespaces, ready, isLoaded]);
return {
t,