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.
- 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
- 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