Skip to content
Snippets Groups Projects
Select Git revision
  • 486a6bf5c5d21fc0b2b3a5f6a4edbfa75c4e1c8c
  • master default protected
  • 1.31
  • 4.28.0
  • 4.27.0
  • 4.26.0
  • 4.25.5
  • 4.25.4
  • 4.25.3
  • 4.25.2
  • 4.25.1
  • 4.25.0
  • 4.24.3
  • 4.24.2
  • 4.24.1
  • 4.24.0
  • 4.23.6
  • 4.23.5
  • 4.23.4
  • 4.23.3
  • 4.23.2
  • 4.23.1
  • 4.23.0
23 results

translations.mjs

Blame
  • translations.mjs 5.83 KiB
    
    
    /**
     * @author schukai GmbH
     */
    
    import {Base} from "../types/base.mjs";
    import {isObject, isString} from "../types/is.mjs";
    import {validateInstance, validateInteger, validateObject, validateString} from "../types/validate.mjs";
    import {Locale, parseLocale} from "./locale.mjs";
    
    export {Translations}
    
    /**
     * With this class you can manage translations and access the keys.
     *
     * ```
     * <script type="module">
     * import {Translations} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@latest/source/i18n/translations.mjs';
     * new Translations()
     * </script>
     * ```
     *
     * @example
     *
     * import {Translations} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@latest/source/i18n/translations.mjs';
     * import {parseLocale} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@latest/source/i18n/locale.mjs';
     *
     * const translation = new Translations(parseLocale('en-GB'));
     *
     * translation.assignTranslations({
     *           text1: "click",
     *           text2: {
     *             'one': 'click once',
     *             'other': 'click n times'
     *           }
     *        });
     *
     * console.log(translation.getText('text1'));
     * // ↦ click
     *
     * console.log(translation.getPluralRuleText('text2',1));
     * // -> click once
     * console.log(translation.getPluralRuleText('text2',2));
     * // -> click n times
     *
     * @since 1.13.0
     * @copyright schukai GmbH
     * @memberOf Monster.I18n
     * @see https://datatracker.ietf.org/doc/html/rfc3066
     */
    class Translations extends Base {
    
        /**
         *
         * @param {Locale} locale
         */
        constructor(locale) {
            super();
    
            if (isString(locale)) {
                locale = parseLocale(locale);
            }
    
            this.locale = validateInstance(locale, Locale);
            this.storage = new Map();
    
        }