Skip to content
Snippets Groups Projects
Verified Commit f24d2a99 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: check the navigator languages too

parent 905e1990
No related branches found
No related tags found
No related merge requests found
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
*/ */
import {parseLocale} from "../i18n/locale.mjs"; import {parseLocale} from "../i18n/locale.mjs";
import {getDocument} from "./util.mjs"; import {getDocument} from "./util.mjs";
import {getGlobalObject} from "../types/global.mjs";
export {getLocaleOfDocument} export {getLocaleOfDocument}
...@@ -47,5 +47,39 @@ function getLocaleOfDocument() { ...@@ -47,5 +47,39 @@ function getLocaleOfDocument() {
} }
} }
let navigatorLanguage = getNavigatorLanguage();
if (navigatorLanguage) {
return parseLocale(navigatorLanguage);
}
return parseLocale(DEFAULT_LANGUAGE); 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;
}
...@@ -142,13 +142,16 @@ class Translations extends Base { ...@@ -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. * 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". * 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. * 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({ * translations.assignTranslations({
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import {expect} from "chai" import {expect} from "chai"
import {Locale} from "../../../../application/source/i18n/locale.mjs";
import {getLocaleOfDocument} from "../../../../application/source/dom/locale.mjs"; import {getLocaleOfDocument} from "../../../../application/source/dom/locale.mjs";
import {initJSDOM} from "../../util/jsdom.mjs"; import {initJSDOM} from "../../util/jsdom.mjs";
...@@ -33,9 +34,23 @@ describe('Attributes', function () { ...@@ -33,9 +34,23 @@ describe('Attributes', function () {
expect(getLocaleOfDocument().toString()).to.be.equal(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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment