Caplin Trader 4.7.1

Class: module:br/i18n/Translator

module:br/i18n/Translator

The class within the br.I18N package that is responsible for translating localization tokens in the form of @{key.name} into translated text.

This class should not be instantiated directly. To access i18n functions use the br/i18n class which returns the br/i18n/I18N accessor class. For example require("br/i18n").i18n("some.i18n.key").

Constructor

new module:br/i18n/Translator()

Methods

convertXMLEntityChars(text)

Converts XML reserved characters (,",',&) into XML entity references.
Parameters:
Name Type Description
text String The string within which to replace localization tokens.
Returns:
A string with every XML reserved character replaced with it's corresponding XML entity reference.

formatDate(date, dateFormat)

Formats a JavaScript date object according to the locale date format string or another passed in date format string. If no date format string is supplied, this function will default to the date format string referenced by the localization property br.i18n.date.format.

Try using the following:

var oTranslator = Translator.getTranslator();
oTranslator.formatDate(myDateObject);

Note that this method will also translate any month names (including abbreviated month names) in the date string to the local equivalents. In order for this translation to work correctly, two sets of localization properties need to be set-up.

For translation of long month names define localization properties of the form: date.month.january=January
For translation of abbreviated month names define localization properties of the form: date.month.short.january=Jan

Parameters:
Name Type Description
date Date A Date object to output as a formatted string.
dateFormat String An optional date format to use. The date formats supported are the same as those used by the Moment.js Date object. Refer to the Moment.js API documentation for further details.
Returns:
The formatted date string.

formatNumber(number)

Formats the number appropriately for the locale.

By specifying a number grouping separator character (',' for example) as the value of the localization property br.i18n.number.grouping.separator, a number such as '1000000' will be formatted as '1,000,000'.

Try using the following:

var oTranslator = Translator.getTranslator();
oTranslator.formatNumber(1000000);
Parameters:
Name Type Description
number Variant A number or a string representing the number.
Throws:
A LocalisedNumber object could not be instantiated from: vNumber.
Type
br.Errors
Returns:
A formatted string representation of the number.

formatTime(time)

Formats the time appropriately for the locale.

By specifying a time separator character (':' for example) as the value of the localization property br.i18n.time.format.separator, a time such as '102001' will be formatted as '10:20:01'.

Try using the following:

var oTranslator = Translator.getTranslator();
oTranslator.formatTime(102001);
Parameters:
Name Type Description
time Variant An integer or string representing the time.
Throws:
A LocalisedTime object could not be instantiated from: vTime.
Type
br.Errors
Returns:
A formatted time string.

getDateFormat()

Returns the current date format string for use in displaying the current date format or for other components that require it to format dates. The string is either the default for the locale or if the user has set a preference then that is returned instead.
Returns:
The date format string, e.g. YYYY-mm-dd.

getMessage(token, templateArgs)

The getMessage method replaces a token with it's translation. Additionally, you can supply extra template arguments that the particular translation might need. For example, a given translations may be ${dialog.message.amountWarning} = "you have [template.key.amount] dollars left in account [template.key.account]". You would call br.i18n("dialog.message.amountWarning", {"template.key.amount":"43234", "template.key.account":"testAccount"}); to get the fully translated message "you have 43234 dollars left in account testAccount"
Parameters:
Name Type Description
token String The token to be translated.
templateArgs Map The *optional* template arguments a translation may require.
Returns:
A string with message tokens replaced with the current locale's messages, possibly with additional substitutions for any template arguments.

getShortDateFormat()

Returns the shorter version of the current date format string for use in displaying the current date format or for other components that require it to format dates. The string is either the default for the locale or if the user has set a preference then that is returned instead.
Returns:
The date format string, e.g. d/m/Y.

parseNumber(number, thousandsSeparator) → {Number}

Parses the number appropriately for the locale, by removing the thousands seperators.

By specifying a number grouping separator character (',' for example) as the value of the localization property br.i18n.number.grouping.separator, a number such as '1,000,000' will be parsed as '1000000'.

Try using the following:

var translator = Translator.getTranslator();
oTranslator.parseNumber('1,000,000.00');
Parameters:
Name Type Description
number String A string representing the number.
thousandsSeparator String (optional) A string representing thousands separator.
Returns:
A parsed number or null if the value can't be parsed.
Type
Number

setLocalizationPreferences(localizationPrefs)

Sets localization preferences for the Translator.
Parameters:
Name Type Description
localizationPrefs Map A map containing the localization preferences.

stripNonNumericCharacters(value)

Strings non numeric characters from the specified string.
Parameters:
Name Type Description
value String the string to strip the non numeric values from.
Returns:
The string without numeric characters

tokenExists(sText)

Returns whether a given localization token is contained in either the current locale or the default locale.

Usage: Translator.getTranslator().tokenExists("br.core.field.start.date")

Parameters:
Name Type Description
sText String The token name
Returns:
true if the localization token exists in the current locale or the default locale's translation set, otherwise false.

translate(sText, sType)

Translate is used to convert raw localization tokens in the form @{key.name} into translated text.

By default this method also converts reserved XML characters (,",',&) into XML entity references (> into > etc). If you require raw text translation without the XML entity reference conversion, pass a type of "text" as an argument to this method.

This:
  • Translator
Parameters:
Name Type Description
sText String The string within which to replace localization tokens.
sType String The type of text to translate (defaults to "xml", pass "text" for translation without XML entity reference conversion).
Returns:
A string with localization tokens replaced with the current locale's messages.