Localizer source
Specializes Montage
Properties
-
availableLocales :Promise source
-
A promise for the locales available in this package. Resolves to an array of strings, each containing a locale tag.
Default Value: - null
-
locale :string source
-
A RFC-5646 language-tag specifying the locale of this localizer. Setting the locale will create a new MessageFormat object with the new locale.
Default Value: - null
-
messageFormat :MessageFormat source
-
The MessageFormat object to use.
Default Value: - null
-
messages :Object source
-
A map from keys to messages.
Default Value: - null
-
messagesPromise :Promise source
-
A promise for the messages property
Default Value: - null
-
require :Function source
-
The require function to use in loadMessages. By default this is set to the global require, meaning that messages will be loaded from the root of the application. To load messages from the root of your package set this to the require function from any class in the package.
Default Value: - global require | null
Methods
-
callDelegateMethod(name) source
-
This method calls the method named with the identifier prefix if it exists. Example: If the name parameter is "shouldDoSomething" and the caller's identifier is "bob", then this method will try and call "bobShouldDoSomething"
Parameters:
Name Type Description name
string Inherited From: -
equals(anObject) → {boolean} source
-
Returns true if two objects are equal, otherwise returns false.
Parameters:
Name Type Description anObject
Object The object to compare for equality.
Inherited From: Returns: boolean
Returns
true
if the calling object andanObject
are identical and theiruuid
properties are also equal. Otherwise, returnsfalse
. -
init(locale) → {Localizer} source
-
Parameters:
Name Type Argument Description locale
string <optional>
The RFC-5646 language tag this localizer should use. Defaults to defaultLocalizer.locale
Returns: Localizer
The Localizer object it was called on.
-
initWithMessages(locale, messages) → {Localizer} source
-
Parameters:
Name Type Description locale
string The RFC-5646 language tag this localizer should use.
messages
Object A map from keys to messages. Each message should either be a string or an object with a "message" property.
Returns: Localizer
The Localizer object it was called on.
-
loadMessages(timeout, callback) → {Promise} source
-
Load messages for the locale
Parameters:
Name Type Argument Default Description timeout
number | boolean <optional>
<nullable>
5000 Number of milliseconds to wait before failing. Set to false for no timeout.
callback
Function <optional>
Called on successful loading of messages. Using the returned promise is recomended.
Returns: Promise
A promise for the messages.
-
localize(key, defaultMessage, defaultOnFail, callback) → {Promise} source
-
Async version of localize.
Waits for the localizer to get messages before localizing the key. Use either the callback or the promise.
defaultLocalizer.localize("hello_name", "Hello, {name}!").then(function (hi) { console.log(hi({name: "World"})); // => "Hello, World!"" console.log(hi()); // => Error: MessageFormat: No data passed to function. });
If the message for the key is "simple", i.e. it does not contain any variables, then the function will implement a custom
toString
function that also returns the message. This means that you can use the function like a string. Example:defaultLocalizer.localize("hello", "Hello").then(function (hi) { // Concatenating an object to a string calls its toString myObject.hello = "" + hi; var y = "The greeting '" + hi + "' is used in this locale"; // textContent only accepts strings and so calls toString button.textContent = hi; // and use as a function also works. var z = hi(); });
Parameters:
Name Type Argument Default Description key
string The key to the string in the messages object.
defaultMessage
string The value to use if key does not exist.
defaultOnFail
string <optional>
true Whether to use the default messages if the messages fail to load.
callback
Function <optional>
Passed the message function.
Returns: Promise
A promise that is resolved with the message function.
-
localizeSync(key, defaultMessage) → {Function} source
-
Localize a key and return a message function.
If the message is a precompiled function then this is returned directly. Otherwise the message string is compiled to a function with <a href="https://github.com/SlexAxton/messageformat.js#readme"> messageformat.js</a>. The resulting function takes an object mapping from variables in the message to their values.
Be aware that if the messages have not loaded yet this method will not return the localized string. See
localize
for method that works whether the messages have loaded or not.Parameters:
Name Type Description key
string The key to the string in the messages object.
defaultMessage
string The value to use if key does not exist.
Returns: Function
A function that accepts an object mapping variables in the message string to values.
Generated from a4679af