new i18n([namespace][, keyRoot])

Creates a new message localizer object.

Parameters

Name Type Optional Description

namespace

string

Yes

The namespace of the messages.

keyRoot

string

Yes

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 Optional Description

pathOfLocales

string

 

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

getLocale

external.getLocale

Yes

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 Optional Description

messageId

string

 

The identifier of the required message.

messageParams

any type

Yes

Optional interpolation parameters of the message.

Value can be repeated.

Throws

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

Returns

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