Browse Source

Fix translation loading bug

isundil 4 years ago
parent
commit
1572076582
3 changed files with 36 additions and 20 deletions
  1. 1 6
      ACPSBooking/routes/index.ts
  2. 27 12
      ACPSBooking/server.ts
  3. 8 2
      ACPSBooking/views/template/loggedpage.pug

+ 1 - 6
ACPSBooking/routes/index.ts

@@ -4,17 +4,12 @@ const router = Router();
 
 function RenderDashboard(req: Request, res: Response) {
     res.renderWithTranslations('dashboard', 'dashboard', {
-        t: req.t,
         username: req.mSession.GetUsername()
     });
 }
 
 function RenderIndex(req: Request, res: Response) {
-    res.renderWithTranslations('index', 'index', {
-        t: (text: string) => {
-            return req.t(text);
-        }
-    });
+    res.renderWithTranslations('index', 'index', {});
 }
 
 router.get('/', (req: Request, res: Response) => {

+ 27 - 12
ACPSBooking/server.ts

@@ -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();
     });

+ 8 - 2
ACPSBooking/views/template/loggedpage.pug

@@ -3,5 +3,11 @@ extends page
 block content
   menu
     ul
-      li Dashboard
-      li menu2
+      li
+        a(href='/')=t('Dashboard')
+      li
+        a(href='/calendar')=t('calendar')
+      li
+        a(href='/trombinoscope')=t('trombinoscope')
+      li
+        a(href='/accounting')=t('accounting')