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( messagesMap)

Do not instantiate this class directly.

Method Summary

Attributes Name and Description
String convertXMLEntityChars(String text)

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

String formatDate(Date date, String format)

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

String formatNumber(String|int num, String thousandsSeparator)

Formats the number appropriately for the locale.

String formatTime(String|int time)

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 token, Map templateTokens)

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, String thousandsSeparator)

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

void setLocalizationPreferences(Map localizationPreferences)

Sets localization preferences for the Translator.

String stripNonNumericCharacters(String value)

Strips non numeric characters from the specified string.

boolean tokenExists(String text)

Returns whether the current locale contains a given localization token.

String translate(String text, String type)

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

Constructor Detail

caplin.i18n.Translator( messagesMap)

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
messagesMap
See
caplin.i18n.Translator#getMessage

Method Detail

String convertXMLEntityChars(String text)

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

Parameters
String text The string within which to replace localization tokens.
Returns
{String} The string with every XML reserved character replaced with its corresponding XML entity reference.

String formatDate(Date date, String format)

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 date A Date object to output as a formatted string.
String format (optional) The 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
{String} The formatted date string.

String formatNumber(String|int num, String thousandsSeparator)

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
String|int num A number or a string representing the number.
String thousandsSeparator (optional) A string representing thousands separator.
Throws
caplin.core.Error
A LocalisedNumber object could not be instantiated from: num.
Returns
{String} A formatted string representation of the number.

String formatTime(String|int time)

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
String|int time An integer or string representing the time.
Throws
caplin.core.Error
A LocalisedTime object could not be instantiated from: time.
Returns
{String} 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
{String} The date format string, e.g. YYYY-mm-dd.

String getLocale()

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

Returns
{String} The local of the current site.

String getMessage(String token, Map templateTokens)

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]'.

To get the fully translated message 'you have 43234 dollars left in account testAccount', you would call:

ct.i18n('dialog.message.amountWarning', {
		'template.key.amount': '43234',
		'template.key.account': 'testAccount'
	});

Parameters
String token The token to be translated.
Map templateTokens (optional) The template arguments a translation may require.
Returns
{String} 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
{String} The date format string, e.g. d/m/Y.

<static> caplin.i18n.Translator getTranslator()

Returns an instance of the Translator.

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

Number parseNumber(Variant sNumber, String thousandsSeparator)

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 string representing the number.
String thousandsSeparator (optional) A string representing thousands separator.
Returns
{Number} A parsed number or null if the value can't be parsed.

void setLocalizationPreferences(Map localizationPreferences)

Sets localization preferences for the Translator.

Parameters
Map localizationPreferences A map containing the localization preferences.

String stripNonNumericCharacters(String value)

Strips non numeric characters from the specified string.

Parameters
String value The string to strip the non numeric values from.
Returns
{String} The string without numeric characters

boolean tokenExists(String text)

Returns whether the current locale contains a given localization token.

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

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

String translate(String text, String type)

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 text The string within which to replace localization tokens.
String type The type of text to translate (defaults to 'xml', pass 'text' for translation without XML entity reference conversion).
Returns
{String} A string with localization tokens replaced with the current locale's messages.