Constructor
new Translations(locale)
Name | Type | Description |
---|---|---|
locale | Locale |
- Since
- 1.13.0
- Copyright
- schukai GmbH
- License
- AGPLv3
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
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. 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."
});
Name | Type | Description |
---|---|---|
translations | object |
- Type:
- Translations
getPluralRuleText(key, count, defaultText) → {string}
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.
Name | Type | Description |
---|---|---|
key | string | |
count | integer | | |
defaultText | string | |
- Type:
- string
getText(key, defaultText) → {string}
Fetches a text using the specified key. If no suitable key is found, defaultText
is taken.
Name | Type | Description |
---|---|---|
key | string | |
defaultText | string | |
key not found
- Type
- Error
- 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."
});
Name | Type | Description |
---|---|---|
key | string | |
text | string | |
value is not a string or object
- Type
- TypeError
- Type:
- Translations