Class: Translator

Translator

new Translator(options)

The Translator is in charge of executing policies. Policies can be provided when constructing or when constructed by using the Translator#add method, policies can be executed using the Translator#execute method. For information on the req object passed into policies, please refer to the documentation of the Request.
Parameters:
Name Type Description
options Object Object with the properties listed below
Properties:
Name Type Description
locales Object.<Object> Hashmap with locales
defaultLocale String Name/key of the default locale
delimiters Array.<String> Array with two items, the start and end delimiter for template variables respectively
options Object The options object passed in
Example
const translator = Translator({
  defaultLocale: 'en-GB',
  delimiters: ['{{', '}}'],
  locales: {
    'en-GB': {
      words: {
        'test': 'Test {{name}}!'
      },
      converters: {}
    }
  }
});

Members

<static> defaults :Object

Defaults for the options passed into the Translator factory
Type:
  • Object
Properties:
Name Type Argument Default Description
delimiters Array.<String> <optional>
['{{', '}}'] The start and end delimiter for template variables

converters :Object

Converters for the current locale
Type:
  • Object

currentLocale :Object

The current locale object
Type:
  • Object

words :Object

Words for the current locale
Type:
  • Object

Methods

add(name, localeDefinition)

Adds one or more languages.
Parameters:
Name Type Description
name String | Object Name of the language, or a hashmap of policies
localeDefinition Object | undefined The definition of the language, has a words and converters property
Example
translator.add('nl-NL', {
  words: {},
  converters: {
    currency() {},
    temperature() {}
  }
});
// or
translator.add({
  'nl-NL': {
    words: {},
    converters: {
      currency() {},
      temperature() {}
    }
  }
});

setLocale(locale)

Sets the current locale
Parameters:
Name Type Description
locale String The locale to set
Example
translator.setLocale('en-GB')

translate(key, data) → {String}

Translates a word with data, key may be a path (like 'basic.yes')
Parameters:
Name Type Argument Default Description
key String The key of the word to translate (may be deep using dots)
data Object <optional>
{} Data to fill the translation with
Returns:
Type
String
Example
// where the translation for 'basic.greet' is 'hello {{model.name}}!'
< translator.translate('basic.greet', {model: {name: 'BOB'}});
> "hello BOB!"
Rik Hoffbauer 2015
Documentation generated by JSDoc 3.4.0 on 2015-12-03T20:18:08+01:00