feat(i18n): add version control for language packages

This commit is contained in:
Darren Yu 2025-09-04 20:30:44 +08:00
parent 08e3df73bd
commit a988e9720d
No known key found for this signature in database
GPG Key ID: 2D69AA5646405984
2 changed files with 49 additions and 30 deletions

View File

@ -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;

View File

@ -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