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 @@
*/
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;
}
......@@ -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({
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment