Internationalisation: Always initialise a default i18n instance (#106552)

always initialise a default i18n instance
This commit is contained in:
Ashley Harrison
2025-06-11 14:44:06 +01:00
committed by GitHub
parent 5d62ec901d
commit 0d695bba92
+8 -2
View File
@@ -30,17 +30,20 @@ export async function loadPluginResources(id: string, language: string, loaders?
}
// exported for testing
export async function initDefaultI18nInstance() {
export function initDefaultI18nInstance() {
// If the resources are not an object, we need to initialize the plugin translations
if (getI18nInstance().options?.resources && typeof getI18nInstance().options.resources === 'object') {
return;
}
await getI18nInstance().use(initReactI18next).init({
const initPromise = getI18nInstance().use(initReactI18next).init({
resources: {},
returnEmptyString: false,
lng: DEFAULT_LANGUAGE, // this should be the locale of the phrases in our source JSX
});
tFunc = getI18nInstance().t;
transComponent = (props: TransProps) => <I18NextTrans shouldUnescape {...props} />;
return initPromise;
}
// exported for testing
@@ -171,6 +174,7 @@ export function addResourceBundle(language: string, namespace: string, resources
}
export const t: TFunction = (id: string, defaultMessage: string, values?: Record<string, unknown>) => {
initDefaultI18nInstance();
if (!tFunc) {
if (process.env.NODE_ENV !== 'test') {
console.warn(
@@ -189,10 +193,12 @@ export const t: TFunction = (id: string, defaultMessage: string, values?: Record
};
export function useTranslate() {
initDefaultI18nInstance();
return { t };
}
export function Trans(props: TransProps) {
initDefaultI18nInstance();
const Component = transComponent ?? I18NextTrans;
return <Component shouldUnescape {...props} />;
}