Formatter

Monster.I18n. Formatter

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

You can create an instance via the monster namespace new Monster.I18n.Formatter().

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

Alternatively, you can also integrate this function individually.

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

Constructor

# new Formatter(object)

Default values for the markers are ${ and }

Parameters:
Name Type Description
object object
Since:
  • 1.26.0
Throws:

value is not a object

Type
TypeError
Example
import {Formatter} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/modules/i18n/formatter.js';
import {Translations} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/modules/i18n/translations.js';

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
Name Type Description
marker object
Properties
Name Type Default Description
open array ["i18n{","${"
close array ["${"
parameter object
Properties
Name Type Default Description
delimiter string "::"
assignment string "="
callbacks object
Properties
Name Type Default Description
i18n function ()=>{}

Methods

# format(text) → {string}

Parameters:
Name Type Description
text string
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