Improve tree shaking and dynamic loading, fix translations in production build. Moved i18n settings to the application's side.
This commit is contained in:
@@ -1,26 +1,23 @@
|
||||
import i18next, { i18n } from 'i18next';
|
||||
import i18next, { type InitOptions, i18n } from 'i18next';
|
||||
import LanguageDetector from 'i18next-browser-languagedetector';
|
||||
import resourcesToBackend from 'i18next-resources-to-backend';
|
||||
import { initReactI18next } from 'react-i18next';
|
||||
|
||||
import { I18N_COOKIE_NAME, getI18nSettings } from './i18n.settings';
|
||||
|
||||
/**
|
||||
* Initialize the i18n instance on the client.
|
||||
* @param settings - the i18n settings
|
||||
* @param resolver - a function that resolves the i18n resources
|
||||
*/
|
||||
export function initializeI18nClient(
|
||||
lng: string | undefined,
|
||||
i18nResolver: (lang: string, namespace: string) => Promise<object>,
|
||||
settings: InitOptions,
|
||||
resolver: (lang: string, namespace: string) => Promise<object>,
|
||||
): Promise<i18n> {
|
||||
const settings = getI18nSettings(lng);
|
||||
|
||||
if (i18next.isInitialized) {
|
||||
return Promise.resolve(i18next);
|
||||
}
|
||||
|
||||
return new Promise<i18n>((resolve, reject) => {
|
||||
void i18next
|
||||
.use(initReactI18next)
|
||||
.use(
|
||||
resourcesToBackend(async (language, namespace, callback) => {
|
||||
const data = await i18nResolver(language, namespace);
|
||||
const data = await resolver(language, namespace);
|
||||
|
||||
return callback(null, data);
|
||||
}),
|
||||
@@ -32,7 +29,7 @@ export function initializeI18nClient(
|
||||
detection: {
|
||||
order: ['htmlTag', 'cookie', 'navigator'],
|
||||
caches: ['cookie'],
|
||||
lookupCookie: I18N_COOKIE_NAME,
|
||||
lookupCookie: 'lang',
|
||||
},
|
||||
interpolation: {
|
||||
escapeValue: false,
|
||||
|
||||
Reference in New Issue
Block a user