Monster.I18n. Formatter

The Formatter extends the Text.Formatter with the possibility to replace the key by a translation.

Constructor

new Formatter(object)

Default values for the markers are ${ and }

.

Default values for the markers are ${ and }

Parameters:
NameTypeDescription
objectobject
Since
  • 1.26.0
License
  • AGPLv3
Throws:

value is not a object

Type
TypeError
Example
import {Formatter} from '@schukai/monster/source/i18n/formatter.mjs';
import {Translations} from '@schukai/monster/source/i18n/translations.mjs';

const translations = new Translations('en')
    .assignTranslations({
        thekey: "${animal} has eaten the ${food}!"
    });

new Formatter({}, translations).format("thekey:animal=dog::food=cake")
// ↦ dog has eaten the cake!

Members

defaults

Properties
NameTypeDescription
markerobject
Properties
NameTypeDefaultDescription
openarray["i18n{","${"
closearray["${"
parameterobject
Properties
NameTypeDefaultDescription
delimiterstring"::"
assignmentstring"="
callbacksobject
Properties
NameTypeDefaultDescription
i18nfunction()=>{}

Methods

format(text) → {string}

Parameters:
NameTypeDescription
textstring
Throws:
  • value is not a string

    Type
    TypeError
  • too deep nesting

    Type
    Error
  • key not found

    Type
    Error
  • the closing marker is missing

    Type
    Error
Returns:
Type: 
string