perf: custom i18next middleware that only initializes when needed
This commit is contained in:
parent
810f5dd84c
commit
d086c053bb
@ -83,7 +83,6 @@
|
|||||||
"form-data": "^4.0.0",
|
"form-data": "^4.0.0",
|
||||||
"i18next": "^22.4.12",
|
"i18next": "^22.4.12",
|
||||||
"i18next-fs-backend": "^2.1.1",
|
"i18next-fs-backend": "^2.1.1",
|
||||||
"i18next-http-middleware": "^3.2.1",
|
|
||||||
"image-size": "^1.0.2",
|
"image-size": "^1.0.2",
|
||||||
"json-bigint": "^1.0.0",
|
"json-bigint": "^1.0.0",
|
||||||
"jsonwebtoken": "^9.0.0",
|
"jsonwebtoken": "^9.0.0",
|
||||||
|
@ -18,11 +18,20 @@
|
|||||||
|
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import i18next from "i18next";
|
import i18next, { TFunction } from "i18next";
|
||||||
import i18nextMiddleware from "i18next-http-middleware";
|
|
||||||
import i18nextBackend from "i18next-fs-backend";
|
import i18nextBackend from "i18next-fs-backend";
|
||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||||
|
namespace Express {
|
||||||
|
interface Request {
|
||||||
|
t: TFunction;
|
||||||
|
language?: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const ASSET_FOLDER_PATH = path.join(__dirname, "..", "..", "..", "assets");
|
const ASSET_FOLDER_PATH = path.join(__dirname, "..", "..", "..", "assets");
|
||||||
|
|
||||||
export async function initTranslation(router: Router) {
|
export async function initTranslation(router: Router) {
|
||||||
@ -34,21 +43,32 @@ export async function initTranslation(router: Router) {
|
|||||||
.filter((x) => x.endsWith(".json"))
|
.filter((x) => x.endsWith(".json"))
|
||||||
.map((x) => x.slice(0, x.length - 5));
|
.map((x) => x.slice(0, x.length - 5));
|
||||||
|
|
||||||
await i18next
|
await i18next.use(i18nextBackend).init({
|
||||||
.use(i18nextBackend)
|
preload: languages,
|
||||||
.use(i18nextMiddleware.LanguageDetector)
|
// debug: true,
|
||||||
.init({
|
fallbackLng: "en",
|
||||||
preload: languages,
|
ns,
|
||||||
// debug: true,
|
backend: {
|
||||||
fallbackLng: "en",
|
loadPath:
|
||||||
ns,
|
path.join(ASSET_FOLDER_PATH, "locales") +
|
||||||
backend: {
|
"/{{lng}}/{{ns}}.json",
|
||||||
loadPath:
|
},
|
||||||
path.join(ASSET_FOLDER_PATH, "locales") +
|
load: "all",
|
||||||
"/{{lng}}/{{ns}}.json",
|
});
|
||||||
},
|
|
||||||
load: "all",
|
|
||||||
});
|
|
||||||
|
|
||||||
router.use(i18nextMiddleware.handle(i18next, {}));
|
router.use((req, res, next) => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
req.t = (key: string | string[], options?: any) => {
|
||||||
|
let lng = "en";
|
||||||
|
if (req.headers["accept-language"]) {
|
||||||
|
lng = req.headers["accept-language"].split(",")[0];
|
||||||
|
}
|
||||||
|
req.language = lng;
|
||||||
|
return i18next.t(key, {
|
||||||
|
...options,
|
||||||
|
lng,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
next();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user