Class jabberwerx.Translator (MINIMAL)
Extends
jabberwerx.JWBase.
Performs localization of strings.
Constructor Attributes | Constructor Name and Description |
---|---|
Creates a new Translator. |
Field Attributes | Field Name and Description |
---|---|
The current locale for this jabberwerx.Translator
|
Method Attributes | Method Name and Description |
---|---|
addTranslation(key, value)
Adds or updates the translation for the given key. |
|
format(istr)
Localizes and formats the given input string. |
|
load(locale)
Loads a translation table for the given locale. |
|
removeTranslation(key)
Removes the translation for the given key. |
- Methods borrowed from class jabberwerx.JWBase:
- destroy, getClassName, graphUnserialized, init, invocation, shouldBeSavedWithGraph, shouldBeSerializedInline, toString, wasUnserialized, willBeSerialized
Creates a new Translator.
NOTE: This class should not be created directly. Instead, use the global localizing method @{link jabberwerx._}.
Adds or updates the translation for the given key.
- Parameters:
- {String} key
- The key to translate on
- {String} value
- The new replacement translation
- Throws:
- {TypeError}
- If {key} or {value} are not valid Strings
Localizes and formats the given input string.
This method attempts to find a localized version of {istr} in a table. If one is found, that localized form is used for the rest of this; otherwise {istr} is used as-is. Then the string is searched for substitutions, and the results are returned.
Substitutions take the form "{<number>}", where <number> is the index of the arugment passed into this method. For example:
var l10n = new jabberwerx.Translator()' var result = l10n.format("Connected to {0} as {1}", client.connectedServer.jid, client.connectedUser.jid);
Returns a string where "{0}" is replaced with the string value of "client.connectedServer.jid" and "{1}" is replaced with the string of "client.connectedUser.jid". If a matching argument is not found, the original pattern is returned (e.g. "{0}").
- Parameters:
- {String} istr
- The input string
- Returns:
- {String} The formatted/localized string
Loads a translation table for the given locale.
The translation table is expected to be a JSON-formatted map of keys to values
:{ "first key" : "The First Key", "service {0} unavailable" : "The service {0} is not available at this time. Please try again later" }
Translation are declared via <link/> elements within the HTML page, with the type 'text/javascript' and the rel 'translation', and optionally an xml:lang to declare the locale it represents. For example, a tranlsation table for American English (en-US) would be declared in the HTML as follows:
< link rel='translation' type='text/javascript' xml:lang='en-US' href='path/to/tranlsation.js'/>
The lookup attempts to find the best match for {locale} using the following algorithm:
- The specific value for {locale}, if specified (e.g. en-US)
- The language-only value for {locale}, if specified (e.g. en)
- A default (no xml:lang declared on <link/>)
- Parameters:
- {String} locale Optional
- The locale to load, or "" to use the platform default.
- Throws:
- {Error}
- If a translation table for {locale} cannot be found or loaded
Removes the translation for the given key.
- Parameters:
- {String} key
- The key to translate on
- Throws:
- {TypeError}
- If {key} or is not a valid String