diff --git a/src/i18n.ts b/src/i18n.ts index b5085e5..97f8f07 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -8,35 +8,8 @@ import LanguageDetector from "i18next-browser-languagedetector"; import ChainedBackend from "i18next-chained-backend"; import LocalStorageBackend from "i18next-localstorage-backend"; -// eslint-disable-next-line @typescript-eslint/ban-ts-ignore -// @ts-ignore -i18n - .use(ChainedBackend) - .use(LanguageDetector) - .use(initReactI18next) - .init({ - fallbackLng: "en-US", - debug: true, - ns: ["common", "application", "dashboard"], - load: "currentOnly", - defaultNS: "application", - interpolation: { - escapeValue: false, - }, - backend: { - backends: process.env.NODE_ENV === "development" ? [Backend] : [LocalStorageBackend, Backend], - backendOptions: [ - { - expirationTime: 7 * 24 * 60 * 60 * 1000, // 7 days - loadPath: "/locales/{{lng}}/{{ns}}.json", - }, - ], - }, - }); - -i18n.on("languageChanged", (lng) => { - document.documentElement.setAttribute("lang", lng); -}); +const isDev = process.env.NODE_ENV === "development"; +isDev && console.log("i18next: set to development mode, localStorage backend disabled."); export const languages = [ { @@ -85,4 +58,49 @@ export const languages = [ }, ]; +// eslint-disable-next-line @typescript-eslint/ban-ts-ignore +// @ts-ignore +i18n + .use(ChainedBackend) + .use(LanguageDetector) + .use(initReactI18next) + .init({ + fallbackLng: languages[0].code, + supportedLngs: languages.map((l) => l.code), + debug: isDev, + ns: ["common", "application", "dashboard"], + load: "currentOnly", + defaultNS: "application", + interpolation: { + escapeValue: false, + }, + backend: { + // https://www.i18next.com/how-to/caching#browser-caching-with-local-storage + // https://github.com/i18next/i18next-localstorage-backend#cache-backend-options + backends: isDev ? [Backend] : [LocalStorageBackend, Backend], + backendOptions: isDev + ? [ + { + /* HttpApi */ + loadPath: "/locales/{{lng}}/{{ns}}.json", + }, + ] + : [ + { + /* LocalStorageBackend */ + expirationTime: 365 * 24 * 60 * 60 * 1000, // 365 days, cache managed by version control + defaultVersion: __BUILD_TIMESTAMP__, + }, + { + /* HttpApi */ + loadPath: "/locales/{{lng}}/{{ns}}.json", + }, + ], + }, + }); + +i18n.on("languageChanged", (lng) => { + document.documentElement.setAttribute("lang", lng); +}); + export default i18n; diff --git a/vite.config.ts b/vite.config.ts index 2b64e2f..4a014c7 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -21,7 +21,7 @@ export default defineConfig({ navigateFallbackDenylist: [/^\/pdfviewer.html/, /^\/api\/(.+)/, /^\/f\/(.+)/, /^\/s\/(.+)/], }, devOptions: { - enabled: true, + enabled: process.env.NODE_ENV !== "production", }, }), viteStaticCopy({ @@ -58,6 +58,7 @@ export default defineConfig({ ], define: { __ASSETS_VERSION__: JSON.stringify(process.env.npm_package_version), + __BUILD_TIMESTAMP__: JSON.stringify(Math.floor(Date.now())), }, build: { outDir: "build", // keep same as v3 with minimal changes