From f24d2a993f81815f5d50d762a1f6dcbc4788ddeb Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Mon, 23 Jan 2023 14:43:11 +0100
Subject: [PATCH] feat: check the navigator languages too

---
 application/source/dom/locale.mjs        | 36 +++++++++++++++++++++++-
 application/source/i18n/translations.mjs | 15 ++++++----
 development/test/cases/dom/locale.mjs    | 15 ++++++++++
 3 files changed, 59 insertions(+), 7 deletions(-)

diff --git a/application/source/dom/locale.mjs b/application/source/dom/locale.mjs
index 1da4ceb9a..27ab797d5 100644
--- a/application/source/dom/locale.mjs
+++ b/application/source/dom/locale.mjs
@@ -6,8 +6,8 @@
  */
 
 import {parseLocale} from "../i18n/locale.mjs";
-
 import {getDocument} from "./util.mjs";
+import {getGlobalObject} from "../types/global.mjs";
 
 export {getLocaleOfDocument}
 
@@ -46,6 +46,40 @@ function getLocaleOfDocument() {
             return new parseLocale(locale)
         }
     }
+    
+    let navigatorLanguage = getNavigatorLanguage();
+    if (navigatorLanguage) {
+        return parseLocale(navigatorLanguage);
+    }
 
     return parseLocale(DEFAULT_LANGUAGE);
 }
+
+/**
+ * @private
+ * @returns {string|undefined|*}
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Navigator/languages
+ */
+const getNavigatorLanguage = () => {
+    const navigator = getGlobalObject('navigator');
+    if (navigator === undefined) {
+        return undefined;
+    }
+
+    if (navigator.hasOwnProperty('language')) {
+        const language = navigator.language;
+        if (typeof language === 'string' && language.length > 0) {
+            return language;
+        }
+        
+    }
+
+    const languages = navigator?.languages;
+    if (Array.isArray(languages) && languages.length>0) {
+        return languages[0];
+    }
+    
+    return undefined;
+
+}
diff --git a/application/source/i18n/translations.mjs b/application/source/i18n/translations.mjs
index 82d9008d0..6f8c15b33 100644
--- a/application/source/i18n/translations.mjs
+++ b/application/source/i18n/translations.mjs
@@ -142,13 +142,16 @@ class Translations extends Base {
     }
 
     /**
-     * This method can be used to transfer overlays from an object. The keys are transferred and the values are entered as text.
-     *
-     * The values can either be character strings or, in the case of texts with plural forms, objects. The plural forms must be stored as text via a standard key "zero", "one", "two", "few", "many" and "other".
-     *
+     * This method can be used to transfer overlays from an object. The keys are transferred, and the values are entered 
+     * as text.
+     * 
+     * The values can either be character strings or, in the case of texts with plural forms, objects. The plural forms
+     * must be stored as text via a standard key "zero", "one", "two", "few", "many" and "other".
+     * 
      * Additionally, the key default can be specified, which will be used if no other key fits.
-     *
-     * In some languages, like for example in german, there is no own more number at the value 0. In these languages the function applies additionally zero.
+     * 
+     * In some languages, like for example in German, there is no own more number at the value 0. In these languages, 
+     * the function applies additionally zero.
      *
      * ```
      * translations.assignTranslations({
diff --git a/development/test/cases/dom/locale.mjs b/development/test/cases/dom/locale.mjs
index 608de1a4a..a58cb96ff 100644
--- a/development/test/cases/dom/locale.mjs
+++ b/development/test/cases/dom/locale.mjs
@@ -2,6 +2,7 @@
 
 import {expect} from "chai"
 
+import {Locale} from "../../../../application/source/i18n/locale.mjs";
 import {getLocaleOfDocument} from "../../../../application/source/dom/locale.mjs";
 import {initJSDOM} from "../../util/jsdom.mjs";
 
@@ -32,10 +33,24 @@ describe('Attributes', function () {
                 node.setAttribute('lang', a);
                 expect(getLocaleOfDocument().toString()).to.be.equal(a);
             })
+            
+            
 
         });
 
 
     });
+    
+    
+    it('return language en', function () {
+        let html = document.getElementsByTagName('html');
+        let node = html.item(0);
+
+        node.removeAttribute('lang');
+        const locale = getLocaleOfDocument();
+        expect(locale).to.be.instanceOf(Locale);
+        expect(locale.localeString).to.be.equal('en-US');
+    })
+    
 
 })
\ No newline at end of file
-- 
GitLab