You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
=> Use ChainedBackend with multiple backends configured.
=> Initialize i18n with use(initReactI18next).use(ChainedBackend).init({...}).
*Observe that loadUrl(url, callback) is triggered twice for the same resource.
Expected Behavior
loadUrl should be called only once per resource.
Actual Behavior
loadUrl gets called twice for the same localization file.
This results in multiple API calls to fetch the same translations, causing redundant network requests.
const getBackendConfig = (
selectedSetupUrl: string,
selectedEnv: string,
getFromSession: { locale: string },
) => {
const locale = getFromSession?.locale || 'en_US'; // Default to 'en_US' if no locale is provided
const domain = getDomainFromUrl(selectedSetupUrl); // Get the domain from the URL
// First backend (dynamic based on the URL and environment)
const dynamicBackend = {
loadPath: getDynamicLoadPath(domain, locale, selectedEnv),
crossDomain: true,
};
// Second backend: i18nextOptions (already defined in your i18nextBackend.js)
const staticBackend = i18nextOptions;
return {
backends: [Fetch, Fetch], // Use two Fetch instances or any other backends you prefer
backendOptions: [dynamicBackend, staticBackend], // The dynamic config and static i18nextOptions
};
};
export default getBackendConfig;
=> Use ChainedBackend with multiple backends configured.
=> Initialize i18n with use(initReactI18next).use(ChainedBackend).init({...}).
*Observe that loadUrl(url, callback) is triggered twice for the same resource.
Expected Behavior
loadUrl should be called only once per resource.
Actual Behavior
loadUrl gets called twice for the same localization file.
This results in multiple API calls to fetch the same translations, causing redundant network requests.
code - const [i18completed, setI18completed] = useState(false);
const [initialized, setInitialized] = useState(false);
const backendConfig = getBackendConfig(selectedSetupUrl, selectedEnv, getFromSession);
import Fetch from 'i18next-fetch-backend';
import { getDynamicLoadPath, i18nextOptions } from '../../i18nextOptions';
import { getDomainFromUrl } from '@/utils/getDomainFromUrl';
const getBackendConfig = (
selectedSetupUrl: string,
selectedEnv: string,
getFromSession: { locale: string },
) => {
const locale = getFromSession?.locale || 'en_US'; // Default to 'en_US' if no locale is provided
const domain = getDomainFromUrl(selectedSetupUrl); // Get the domain from the URL
// First backend (dynamic based on the URL and environment)
const dynamicBackend = {
loadPath: getDynamicLoadPath(domain, locale, selectedEnv),
crossDomain: true,
};
// Second backend: i18nextOptions (already defined in your i18nextBackend.js)
const staticBackend = i18nextOptions;
return {
backends: [Fetch, Fetch], // Use two Fetch instances or any other backends you prefer
backendOptions: [dynamicBackend, staticBackend], // The dynamic config and static i18nextOptions
};
};
export default getBackendConfig;
if (!i18completed && !initialized) {
setInitialized(true);
i18n
.use(initReactI18next)
.use(ChainedBackend)
.init({
backend: {
backends: backendConfig.backends, // Multiple backends
backendOptions: backendConfig.backendOptions,
},
fallbackLng: 'en_US',
lng: getFromSession?.locale || 'en_US',
preload: ['en_US'],
debug: true,
react: {
useSuspense: false,
},
detection: {
order: [
'querystring',
'cookie',
'localStorage',
'navigator',
'htmlTag',
'path',
'subdomain',
],
caches: ['localStorage', 'cookie'],
},
interpolation: {
escapeValue: false,
},
})
.then(() => {
setI18completed(true);
});
}
version - "i18next": "^23.11.5",
The text was updated successfully, but these errors were encountered: