Translations

Monster.I18n. Translations

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

You can call the method via the monster namespace new Monster.I18n.Translations().

<script type="module">
import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/monster.js';
new Monster.I18n.Translations()
</script>

Alternatively, you can also integrate this function individually.

<script type="module">
import {Translations} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/modules/i18n/translations.js';
new Translations()
</script>

Constructor

# new Translations(locale)

Parameters:
Name Type Description
locale Locale
Since:
  • 1.13.0
Example
import {Translations} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/modules/i18n/translations.js';
import {parseLocale} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/modules/i18n/locale.js';

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

Methods

# assignTranslations(translations) → {Translations}

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:
Name Type Description
translations object
Returns:
Type
Translations

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

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:
Name Type Description
key string
count integer | count
defaultText string | undefined
Returns:
Type
string

# getText(key, defaultText) → {string}

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

Parameters:
Name Type Description
key string
defaultText string | 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."
});
Parameters:
Name Type Description
key string
text string | object
Throws:

value is not a string or object

Type
TypeError
Returns:
Type
Translations