Class: i18n

bo. i18n

Provide methods to get localized messages.

new i18n(namespace, keyRoot)

Creates a new message localizer object.

Parameters:
Name Type Argument Default Description
namespace string <optional>
$default

The namespace of the messages.

keyRoot string <optional>

The key root of the messages.

Throws:
  • i18n error: The namespace must be a string value or null.
  • i18n error: The key root must be a string value or null.

Methods

<static> initialize(pathOfLocales, getLocale)

Reads the localized messages of the project.

Parameters:
Name Type Argument Description
pathOfLocales string

The relative path of the directory that contains the project messages.

getLocale external.getLocale <optional>

A function that returns the current locale.

Throws:
  • i18n error: The path of locales must be a non-empty string.
  • i18n error: The path of locales is not a valid directory path.
  • i18n error: The locale getter must be a function.

get(messageId, messageParams) → {string}

Gets a localized message of a given identifier. The message identifier has the pattern: [locale*][namespace:]key1[[.key2]...] Examples:

australia
Simple key with default namespace and current locale.
europe.spain.andalusia
Extended key with default namespace and current locale.
geography:australia
Simple key with specified namespace and current locale.
geography:europe.spain.andalusia
Extended key with specified namespace and current locale.
hu*australia
Simple key with default namespace and specified language.
hu-HU*europe.spain.andalusia
Extended key with default namespace and specified language with region.
hu*geography:australia
Simple key with specified namespace and specified language.
hu-HU*geography:europe.spain.andalusia
Extended key with specified namespace and specified language with region.
If localizer is created with a namespace and the message identifier omits the namespace, then the localizer namespace will applied. Examples for initial namespace economy:
australia
It is interpreted as economy:australia
hu-HU*europe.spain.andalusia
It is interpreted as hu-HU*economy:europe.spain.andalusia
geography:australia
It remains geography:australia
hu-HU*geography:europe.spain.andalusia
It remains hu-HU*geography:europe.spain.andalusia
If localizer is created with a key root and the message identifier omits the namespace, then the key root is inserted before the key part of the specified identifier. Examples for initial key root earth:
australia
It is interpreted as earth.australia
hu-HU*europe.spain.andalusia
It is interpreted as hu-HU*earth.europe.spain.andalusia
geography:australia
It remains geography:australia
hu-HU*geography:europe.spain.andalusia
It remains hu-HU*geography:europe.spain.andalusia
Parameters:
Name Type Argument Description
messageId string

The identifier of the required message.

messageParams * <optional>
<repeatable>

Optional interpolation parameters of the message.

Throws:
i18n error: The message key must be a non-empty string.
Returns:

The localized message for the current locale, if not found then the message for the default locale, otherwise the message key.

Type
string
If you find bugs or have feature requests, please let us know.

JavaScript business objects ♦ Copyright © 2014-2016 Logikum, Ltd.
Documentation generated by JSDoc 3.2.2 on 2015. December 31. using the DocStrap template.