Class
caplin.i18n

Translator

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

Constructor Summary

Attributes Name and Description
caplin.i18n.Translator( mMessages)

Do not instantiate this class directly.

Method Summary

Attributes Name and Description
String convertXMLEntityChars(String sText)

Converts XML reserved characters (<,>,",',&) into XML entity references.

String formatDate(Date oDate, String sDateFormat)

Formats a JavaScript date object according to the locale date format string or another passed in date format string.

String formatNumber(Variant vNumber, sThousandsSeparator)

Formats the number appropriately for the locale.

String formatTime(Variant vTime)

Formats the time appropriately for the locale.

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.

String getLocale()

Returns the locale that the current site has been loaded in.

String getMessage(String sToken, Map mTemplateArgs)

The getMessage method replaces a token with it's translation.

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

<static> caplin.i18n.Translator getTranslator()

Returns an instance of the Translator.

Number parseNumber(Variant sNumber, sThousandsSeparator)

Parses the number appropriately for the locale, by removing the ThousandsSeperators and decimal points.

void setLocalizationPreferences(Map mLocalizationPrefs)

Sets localization preferences for the Translator.

String stripNonNumericCharacters(String sValue)

Strings non numeric characters from the specified string.

boolean tokenExists(String sText)

Returns whether the current locale contains a given localization token.

String translate(String sText, String sType)

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

Constructor Detail

caplin.i18n.Translator( mMessages)

Do not instantiate this class directly. To access the localization token translator use the global ct.i18n(token, args) function which maps to the getMessage() function.

Parameters
mMessages
See
caplin.i18n.Translator#getMessage

Method Detail

String convertXMLEntityChars(String sText)

Converts XML reserved characters (<,>,",',&) into XML entity references.

Parameters
String sText The string within which to replace localization tokens.
Returns
A string with every XML reserved character replaced with it's corresponding XML entity reference.

String formatDate(Date oDate, String sDateFormat)

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 ct.i18n.date.format.

Try using the following:

var oTranslator = caplin.i18n.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
Date oDate A Date object to output as a formatted string.
String sDateFormat 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.

String formatNumber(Variant vNumber, sThousandsSeparator)

Formats the number appropriately for the locale.

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

Try using the following:

var oTranslator = caplin.i18n.Translator.getTranslator();
oTranslator.formatNumber(1000000);

Parameters
Variant vNumber A number or a string representing the number.
sThousandsSeparator
Throws
caplin.core.Error
A LocalisedNumber object could not be instantiated from: vNumber.
Returns
A formatted string representation of the number.

String formatTime(Variant vTime)

Formats the time appropriately for the locale.

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

Try using the following:

var oTranslator = caplin.i18n.Translator.getTranslator();
oTranslator.formatTime(102001);

Parameters
Variant vTime An integer or string representing the time.
Throws
caplin.core.Error
A LocalisedTime object could not be instantiated from: vTime.
Returns
A formatted time string.

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.

String getLocale()

Returns the locale that the current site has been loaded in.

Returns
the local of the current site.

String getMessage(String sToken, Map mTemplateArgs)

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 ct.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
String sToken The token to be translated.
Map mTemplateArgs 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.

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

<static> caplin.i18n.Translator getTranslator()

Returns an instance of the Translator.

Returns
The Translator for i18n translations for use within this instance of the application.

Number parseNumber(Variant sNumber, sThousandsSeparator)

Parses the number appropriately for the locale, by removing the ThousandsSeperators and decimal points.

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

Try using the following:

var oTranslator = caplin.i18n.Translator.getTranslator();
oTranslator.parseNumber(1,000,000.00);

Parameters
Variant sNumber A a string representing the number.
sThousandsSeparator
Returns
A parsed number or null if the value can't be parsed.

void setLocalizationPreferences(Map mLocalizationPrefs)

Sets localization preferences for the Translator.

Parameters
Map mLocalizationPrefs A map containing the localization preferences.

String stripNonNumericCharacters(String sValue)

Strings non numeric characters from the specified string.

Parameters
String sValue the string to strip the non numeric values from.
Returns
The string without numeric characters

boolean tokenExists(String sText)

Returns whether the current locale contains a given localization token.

Usage: caplin.i18n.Translator.getTranslator().tokenExists("ct.core.field.start.date")

Parameters
String sText The token name
Returns
true if the localization token exists in the current locale's translation set, otherwise false.

String translate(String sText, String 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.

Parameters
String sText The string within which to replace localization tokens.
String sType 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.