DefaultLocalizer source
Specializes Localizer
Properties
-
availableLocales :Promise source
-
A promise for the locales available in this package. Resolves to an array of strings, each containing a locale tag.
Inherited From: Default Value: - null
-
delegate :Object source
-
Delegate to get the default locale. Should implement a
getDefaultLocale
method that returns a language-tag string that can be passed to localeDefault Value: - null
-
messageFormat :MessageFormat source
-
The MessageFormat object to use.
Inherited From: Default Value: - null
-
messages :Object source
-
A map from keys to messages.
Inherited From: Default Value: - null
-
messagesPromise :Promise source
-
A promise for the messages property
Inherited From: 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.
Inherited From: 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
. -
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.
Inherited From: 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.
Inherited From: 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.
Inherited From: 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.
Inherited From: Returns: Function
A function that accepts an object mapping variables in the message string to values.
-
reset() → {string} source
-
Reset the saved locale back to default by using the steps above.
Returns: string
the reset locale
Generated from a4679af