diff --git a/application/source/dom/locale.mjs b/application/source/dom/locale.mjs index 1da4ceb9a46d708365830193b3ccb72c32cf3e52..27ab797d5f741fac7595c8879dfd9a3bbcc18cc2 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 82d9008d09e20d3aaa7513e4bead3e96f71b61d4..6f8c15b3327836fa6cc8dbbaadeef3f78068547b 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 608de1a4a88d778583ac8aea2eafd0d67412ed68..a58cb96ff89977ebaa7041c7bbca52c8bba74c96 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