From ad628bed170289ff235aca449fbae17663e18863 Mon Sep 17 00:00:00 2001
From: Dominik Hebeler <dominik@hebeler.club>
Date: Thu, 13 Jul 2023 11:17:23 +0200
Subject: [PATCH] fixed order of detections

---
 pass/app.js                          |  7 +------
 pass/app/Langdetector.js             | 19 +++++++++++++------
 pass/routes/key.js                   |  2 +-
 pass/views/help/faq.ejs              |  2 +-
 pass/views/index.ejs                 |  2 +-
 pass/views/templates/page_footer.ejs |  2 +-
 6 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/pass/app.js b/pass/app.js
index f524bb6..d12e66d 100644
--- a/pass/app.js
+++ b/pass/app.js
@@ -37,12 +37,7 @@ i18next
     debug: false,
     // Lang Detector Options
     detection: {
-      order: ["cookie", "mg_detection"],
-      lookupCookie: "web_setting_m",
-      lookupFromPathIndex: 0,
-      convertDetectedLanguage: (lng) => {
-        return lng.replace("_", "-");
-      }
+      order: ["mg_detection"],
     },
     // FS Backend Options
     backend: {
diff --git a/pass/app/Langdetector.js b/pass/app/Langdetector.js
index 04bc7b6..af6ac3d 100644
--- a/pass/app/Langdetector.js
+++ b/pass/app/Langdetector.js
@@ -4,19 +4,26 @@ module.exports = {
         // Cookie is checked at this point
         // Next detection in order is the request path
         let path = req.path.replace(/^\/+/, "").replace(/\/+$/, "").split("/");
-        let path_matches = path[0].match(/^([a-z]{2})-([A-Z]{2})/);
-        if (path.length > 0 && path_matches) {
+        let lang_matches = path[0].match(/^([a-z]{2})-([A-Z]{2})/);
+        if (!lang_matches) {
+            // Check if a lang cookie is defined
+            let cookie = req.cookies["web_setting_m"];
+            if (cookie && cookie.match()) {
+                lang_matches = cookie.match(/^([a-z]{2})-([A-Z]{2})/);
+            }
+        }
+        if (path.length > 0 && lang_matches) {
             let path_tool = require("path");
             let fs = require("fs");
-            let lang_folder = path_tool.join(__dirname, "../lang", path[0]);
+            let lang_folder = path_tool.join(__dirname, "../lang", lang_matches[0]);
             // Check if translation exists for full locale
             if (fs.existsSync(lang_folder)) {
-                return path[0];
+                return lang_matches[0];
             }
             // Check if translation exists for language part of locale
-            lang_folder = path_tool.join(__dirname, "../lang", path_matches[1]);
+            lang_folder = path_tool.join(__dirname, "../lang", lang_matches[1]);
             if (fs.existsSync(lang_folder)) {
-                return path[0];
+                return lang_matches[0];
             }
         }
 
diff --git a/pass/routes/key.js b/pass/routes/key.js
index 8d1efc9..07f72d2 100644
--- a/pass/routes/key.js
+++ b/pass/routes/key.js
@@ -122,7 +122,7 @@ router.use("/:key", param("key").isUUID(4), async (req, res, next) => {
   }
 
   let metager_url = "https://metager.de";
-  if (req.lng !== "de") {
+  if (!req.lng.match(/^de/)) {
     metager_url = "https://metager.org";
   }
   metager_url += "/meta/settings/load-settings?";
diff --git a/pass/views/help/faq.ejs b/pass/views/help/faq.ejs
index 0bbbc61..ac5a707 100644
--- a/pass/views/help/faq.ejs
+++ b/pass/views/help/faq.ejs
@@ -51,6 +51,6 @@
       <p><%= req.t("faq.faqs.6.description", {ns: "help"}) _%></p>
     </details>
   </div>
-  <h3><%- req.t("faq.more-questions", {ns: "help", contactlink: req.lng === "de" ? "https://metager.de/kontakt" : "https://metager.org/kontakt"}) _%></h3>
+  <h3><%- req.t("faq.more-questions", {ns: "help", contactlink: req.lng.match(/^de/) ? "https://metager.de/kontakt" : "https://metager.org/kontakt"}) _%></h3>
 </div>
 <%- include('../templates/page_footer'); -%>
\ No newline at end of file
diff --git a/pass/views/index.ejs b/pass/views/index.ejs
index 9a47ef6..2d6c23b 100644
--- a/pass/views/index.ejs
+++ b/pass/views/index.ejs
@@ -27,7 +27,7 @@
 </div>
 <div id="advantages">
   <div class="advantage" id="no-ads">
-    <%_ if(req.lng == "de" ) { _%>
+    <%_ if(req.lng.match(/^de/)) { _%>
     <img src="<%= baseDir _%>/images/adfree.svg" alt="Image depicting money" />
     <%_ }else { _%>
     <img src="<%= baseDir _%>/images/adfree_en.svg" alt="Image depicting money" />
diff --git a/pass/views/templates/page_footer.ejs b/pass/views/templates/page_footer.ejs
index 0e1705f..1b76346 100644
--- a/pass/views/templates/page_footer.ejs
+++ b/pass/views/templates/page_footer.ejs
@@ -6,7 +6,7 @@
     </ul>
   </footer>
 
-  <button class="open-chat button hidden" data-title="<%= req.t("chat.title", {ns: "pageheader"})%>" data-chatid="<%= req.lng == "de"? 1 : 2 %>">
+  <button class="open-chat button hidden" data-title="<%= req.t("chat.title", {ns: "pageheader"})%>" data-chatid="<%= req.lng.match(/^de/) ? 1 : 2 %>">
     <img src="<%= baseDir %>/images/chat.svg" />
     <%= req.t("chat.button", {ns: "pageheader"}) _%>
   </button>
-- 
GitLab