From f1ada36cc8782c9d793591bdda59efb7c427cbc0 Mon Sep 17 00:00:00 2001 From: Arnei Date: Mon, 16 Nov 2026 12:32:54 +0100 Subject: [PATCH] Load translations on demand Bundle only the English translation and fetch other languages when they are used, which makes the bundle about 0.9 MB smaller. This commit was AI generated --- package-lock.json | 10 ------ package.json | 1 - src/i18n/i18n.ts | 89 ++++++++++++++++++++++++----------------------- src/index.tsx | 28 ++++++++------- 4 files changed, 62 insertions(+), 66 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9ff57ef91b..0a9b74da1d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,6 @@ "http-proxy-middleware": "^4.2.1", "i18next": "26.5.11", "i18next-browser-languagedetector": "^8.2.3", - "i18next-http-backend": "^4.0.6", "lodash": "^4.17.23", "react": "^19.3.1", "react-chartjs-2": "^5.3.1", @@ -3819,15 +3818,6 @@ "@babel/runtime": "^7.29.2" } }, - "node_modules/i18next-http-backend": { - "version": "4.0.6", - "resolved": "https://registry.npmjs.org/i18next-http-backend/-/i18next-http-backend-4.0.6.tgz", - "integrity": "sha512-k5OPDXAQH9CZ2Ju2DDw1ZC4WiYmjKUjOE4lcd51tNQyWjVDfpuwPcE9Y5VzDpz1tR1Z4wxkkj4H+ImxgwbfpeQ==", - "license": "MIT", - "engines": { - "node": ">=18" - } - }, "node_modules/ignore": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz", diff --git a/package.json b/package.json index da4fe3d86d..a60c1c294c 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,6 @@ "http-proxy-middleware": "^4.2.1", "i18next": "26.5.11", "i18next-browser-languagedetector": "^8.2.3", - "i18next-http-backend": "^4.0.6", "lodash": "^4.17.23", "react": "^19.3.1", "react-chartjs-2": "^5.3.1", diff --git a/src/i18n/i18n.ts b/src/i18n/i18n.ts index cb2b2ea49e..f0a0f3ad83 100644 --- a/src/i18n/i18n.ts +++ b/src/i18n/i18n.ts @@ -1,51 +1,51 @@ -import i18n, { FormatterModule } from "i18next"; +import i18n, { BackendModule, FormatterModule } from "i18next"; import { initReactI18next } from "react-i18next"; -import HttpBackend, { HttpBackendOptions } from "i18next-http-backend"; import LanguageDetector from "i18next-browser-languagedetector"; -// import language files +// The translation of the default language is part of the bundle, as it is also the fallback for all other languages import enUSTrans from "./org/opencastproject/adminui/languages/lang-en_US.json"; -import enGBTrans from "./org/opencastproject/adminui/languages/lang-en_GB.json"; -import daDKTrans from "./org/opencastproject/adminui/languages/lang-da_DK.json"; -import deDETrans from "./org/opencastproject/adminui/languages/lang-de_DE.json"; -import elGRTrans from "./org/opencastproject/adminui/languages/lang-el_GR.json"; -import esESTrans from "./org/opencastproject/adminui/languages/lang-es_ES.json"; -import frFRTrans from "./org/opencastproject/adminui/languages/lang-fr_FR.json"; -import glESTrans from "./org/opencastproject/adminui/languages/lang-gl_ES.json"; -import heILTrans from "./org/opencastproject/adminui/languages/lang-he_IL.json"; -import itITTrans from "./org/opencastproject/adminui/languages/lang-it_IT.json"; -import nlNLTrans from "./org/opencastproject/adminui/languages/lang-nl_NL.json"; -import plPLTrans from "./org/opencastproject/adminui/languages/lang-pl_PL.json"; -import slSITrans from "./org/opencastproject/adminui/languages/lang-sl_SI.json"; -import svSETrans from "./org/opencastproject/adminui/languages/lang-sv_SE.json"; -import trTRTrans from "./org/opencastproject/adminui/languages/lang-tr_TR.json"; -import zhCNTrans from "./org/opencastproject/adminui/languages/lang-zh_CN.json"; -import zhTWTrans from "./org/opencastproject/adminui/languages/lang-zh_TW.json"; import { getCurrentLanguageInformation } from "../utils/utils"; import { format as dateFnsFormat } from "date-fns/format"; -// Assignment of language code to translation file +// Assignment of language code to translation file, which is only loaded once the language is used // !!! If translation file of a new language is added, please add assignment here, too !!! -const resources = { - "en-US": { translation: enUSTrans }, - "en-GB": { translation: enGBTrans }, - da: { translation: daDKTrans }, - de: { translation: deDETrans }, - el: { translation: elGRTrans }, - es: { translation: esESTrans }, - fr: { translation: frFRTrans }, - gl: { translation: glESTrans }, - he: { translation: heILTrans }, - it: { translation: itITTrans }, - nl: { translation: nlNLTrans }, - pl: { translation: plPLTrans }, - sl: { translation: slSITrans }, - sv: { translation: svSETrans }, - tr: { translation: trTRTrans }, - "zh-CN": { translation: zhCNTrans }, - "zh-TW": { translation: zhTWTrans }, -} as const; +const translations: Record Promise<{ default: object }>> = { + "en-GB": () => import("./org/opencastproject/adminui/languages/lang-en_GB.json"), + da: () => import("./org/opencastproject/adminui/languages/lang-da_DK.json"), + de: () => import("./org/opencastproject/adminui/languages/lang-de_DE.json"), + el: () => import("./org/opencastproject/adminui/languages/lang-el_GR.json"), + es: () => import("./org/opencastproject/adminui/languages/lang-es_ES.json"), + fr: () => import("./org/opencastproject/adminui/languages/lang-fr_FR.json"), + gl: () => import("./org/opencastproject/adminui/languages/lang-gl_ES.json"), + he: () => import("./org/opencastproject/adminui/languages/lang-he_IL.json"), + it: () => import("./org/opencastproject/adminui/languages/lang-it_IT.json"), + nl: () => import("./org/opencastproject/adminui/languages/lang-nl_NL.json"), + pl: () => import("./org/opencastproject/adminui/languages/lang-pl_PL.json"), + sl: () => import("./org/opencastproject/adminui/languages/lang-sl_SI.json"), + sv: () => import("./org/opencastproject/adminui/languages/lang-sv_SE.json"), + tr: () => import("./org/opencastproject/adminui/languages/lang-tr_TR.json"), + "zh-CN": () => import("./org/opencastproject/adminui/languages/lang-zh_CN.json"), + "zh-TW": () => import("./org/opencastproject/adminui/languages/lang-zh_TW.json"), +}; + +// Loads the translation file of a language when i18next asks for it +const translationBackend: BackendModule = { + type: "backend", + init() { }, + read(language, _namespace, callback) { + const load = translations[language]; + // Nothing to load for regions of a language, like "de-CH", as they fall back to the language itself + if (!load) { + callback(null, {}); + return; + } + load().then( + translation => callback(null, translation.default), + (error: Error) => callback(error, null), + ); + }, +}; const myFormatter: FormatterModule = { type: "formatter", @@ -66,13 +66,16 @@ const myFormatter: FormatterModule = { }; // Configuration of i18next -i18n - .use(HttpBackend) +// The promise resolves as soon as the translations of the language of the user are available +export const i18nReady = i18n + .use(translationBackend) .use(LanguageDetector) .use(initReactI18next) .use(myFormatter) - .init({ - resources, + .init({ + resources: { "en-US": { translation: enUSTrans } }, + // Without this, i18next would not load the other languages with the backend + partialBundledLanguages: true, fallbackLng: "en-US", debug: false, diff --git a/src/index.tsx b/src/index.tsx index 0515fb1b6d..24a25f9ff0 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -11,7 +11,7 @@ import { Provider } from "react-redux"; import store from "./store"; // import i18n (needs to be bundled) -import "./i18n/i18n"; +import { i18nReady } from "./i18n/i18n"; // import css files for certain libraries import "react-datepicker/dist/react-datepicker.css"; @@ -34,14 +34,18 @@ if (!container) { } const root = ReactDOMClient.createRoot(container); -root.render( - - - loading...} persistor={persistor}> - - - - - - , -); +// Wait for the translations of the language of the user, so the app is not shown in English first. +// If they cannot be loaded, the app is shown in English. +i18nReady.finally(() => { + root.render( + + + loading...} persistor={persistor}> + + + + + + , + ); +});