|
|
@@ -24,7 +24,7 @@ declare global {
|
|
|
interface Request {
|
|
|
mSession: Session
|
|
|
mCookies: Map<string, string>
|
|
|
- t: (s: string) => string // i18n translation function
|
|
|
+ t: (s: string, opts: any|undefined) => string // i18n translation function
|
|
|
}
|
|
|
|
|
|
interface Response {
|
|
|
@@ -34,6 +34,12 @@ declare global {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+function translate(t: any, ns: string, str: string, args: any | undefined = undefined) {
|
|
|
+ args = args || {};
|
|
|
+ args.ns = args.ns || ns;
|
|
|
+ return t(str, args);
|
|
|
+}
|
|
|
+
|
|
|
// Check config
|
|
|
(async _ => {
|
|
|
await i18next.use(i18nextMiddleware.LanguageDetector)
|
|
|
@@ -88,16 +94,25 @@ declare global {
|
|
|
// Security and session setup
|
|
|
app.use((req: express.Request, res: express.Response, next) => {
|
|
|
res.i18next = i18next;
|
|
|
- res.renderWithTranslations = async (translationNs: string, renderView: string, args: any) => {
|
|
|
- await i18next.loadNamespaces(translationNs);
|
|
|
- i18next.setDefaultNamespace(translationNs);
|
|
|
+ res.renderWithTranslations = async (defaultNs: string, renderView: string, args: any) => {
|
|
|
+ await i18next.loadNamespaces(defaultNs);
|
|
|
args = args || {};
|
|
|
- args["t"] = req.t;
|
|
|
+ args["t"] = (a1: string, a2: any | undefined, a3: any | undefined): string => {
|
|
|
+ if (a3)
|
|
|
+ return translate(req.t, a1, a2, a3);
|
|
|
+ if (a2) {
|
|
|
+ if (typeof a2 === 'string')
|
|
|
+ return translate(req.t, a1, a2);
|
|
|
+ else
|
|
|
+ return translate(req.t, defaultNs, a1, a2);
|
|
|
+ }
|
|
|
+ return req.t(a1, { ns: defaultNs });
|
|
|
+ };
|
|
|
res.render(renderView, args);
|
|
|
};
|
|
|
|
|
|
req.mCookies = new Map();
|
|
|
- (req.headers?.cookie || "").split(";").forEach(i => {
|
|
|
+ (req.headers?.cookie || "").split(";").filter(i => i.length).forEach(i => {
|
|
|
let keyValue = i.split("=", 2);
|
|
|
req.mCookies.set(keyValue[0].trim(), keyValue[1].trim());
|
|
|
});
|
|
|
@@ -108,12 +123,12 @@ declare global {
|
|
|
Security.TryLoginApiKey(req.query["API_KEY"].toString()).then(user => {
|
|
|
req.mSession.Login(user);
|
|
|
})
|
|
|
- .catch(e => {
|
|
|
- let err: any = new Error("Access denied");
|
|
|
- err['status'] = 403;
|
|
|
- next(err);
|
|
|
- return;
|
|
|
- });
|
|
|
+ .catch(e => {
|
|
|
+ let err: any = new Error("Access denied");
|
|
|
+ err['status'] = 403;
|
|
|
+ next(err);
|
|
|
+ return;
|
|
|
+ });
|
|
|
}
|
|
|
next();
|
|
|
});
|