Skip to content
Snippets Groups Projects
Select Git revision
  • a2333885a8290305fbb97ca52b256a0a446890ba
  • master default protected
  • 1.31
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
  • 4.32.0
  • 4.31.0
  • 4.30.1
  • 4.30.0
  • 4.29.1
  • 4.29.0
  • 4.28.0
  • 4.27.0
  • 4.26.0
  • 4.25.5
  • 4.25.4
  • 4.25.3
  • 4.25.2
  • 4.25.1
23 results

websocket.mjs

Blame
  • locale.mjs 2.24 KiB
    /**
     * Copyright schukai GmbH and contributors 2022. All Rights Reserved.
     * Node module: @schukai/monster
     * This file is licensed under the AGPLv3 License.
     * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
     */
    
    import { parseLocale } from "../i18n/locale.mjs";
    import { getDocument } from "./util.mjs";
    import { getGlobalObject } from "../types/global.mjs";
    
    export { getLocaleOfDocument };
    
    /**
     * @private
     * @type {string}
     */
    const DEFAULT_LANGUAGE = "en";
    
    /**
     * With this function you can read the language version set by the document.
     * For this the attribute `lang` in the html tag is read. If no attribute is set, `en` is used as default.
     * Alternatively, the language version of the browser is used.
     *
     * ```html
     * <html lang="en">
     * ```
     *
     * You can call the function via `getLocaleOfDocument()`.
     *
     * @license AGPLv3
     * @since 1.13.0
     * @copyright schukai GmbH
     * @memberOf Monster.DOM
     * @throws {TypeError} value is not a string
     * @throws {Error} unsupported locale
     * @summary Tries to determine the locale used
     */
    function getLocaleOfDocument() {
        const document = getDocument();
    
        let html = document.querySelector("html");
        if (html instanceof HTMLElement && html.hasAttribute("lang")) {
            let locale = html.getAttribute("lang");
            if (locale) {
                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")) {