Monster.I18n. Translations

With this class you can manage translations and access the keys.

Constructor

new Translations(locale)

Parameters:
NameTypeDescription
localeLocale
Since
  • 1.13.0
License
  • AGPLv3
Example
import {Translations} from '@schukai/monster/source/i18n/translations.mjs';
import {parseLocale} from '@schukai/monster/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

Members

(static) instanceSymbol

This method is called by the instanceof operator.

This method is called by the instanceof operator.

Since
  • 3.27.0

Methods

assignTranslations(translations) → {Translations}

This method can be used to transfer overlays from an object.

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.

translations.assignTranslations({
  "text1": "Make my day!",
  "text2": "I'll be back!",
  "text6": {
    "zero": "There are no files on Disk.",
    "one": "There is one file on Disk.",
    "other": "There are files on Disk."
    "default": "There are files on Disk."
});
Parameters:
NameTypeDescription
translationsobject
Returns:
Type: 
Translations

getPluralRuleText(key, count, defaultText) → {string}

A number count can be passed to this method.

A number count can be passed to this method. In addition to a number, one of the keywords can also be passed directly. "zero", "one", "two", "few", "many" and "other". Remember: not every language has all rules.

The appropriate text for this number is then selected. If no suitable key is found, defaultText is taken.

Parameters:
NameTypeDescription
keystring
countinteger | string
defaultTextstring | undefined
Returns:
Type: 
string

getText(key, defaultText) → {string}

Fetches a text using the specified key.

Fetches a text using the specified key. If no suitable key is found, defaultText is taken.

Parameters:
NameTypeDescription
keystring
defaultTextstring | undefined
Throws:

key not found

Type
Error
Returns:
Type: 
string

setText(key, text) → {Translations}

Set a text for a key

translations.setText("text1", "Make my day!");
// plural rules
translations.setText("text6", {
    "zero": "There are no files on Disk.",
    "one": "There is one file on Disk.",
    "other": "There are files on Disk."
    "default": "There are files on Disk."
});
.

Set a text for a key

translations.setText("text1", "Make my day!");
// plural rules
translations.setText("text6", {
    "zero": "There are no files on Disk.",
    "one": "There is one file on Disk.",
    "other": "There are files on Disk."
    "default": "There are files on Disk."
});
Parameters:
NameTypeDescription
keystring
textstring | object
Throws:

value is not a string or object

Type
TypeError
Returns:
Type: 
Translations