Skip to main content
Version: 1.20.1

s_i18n

This class provides methods that return translated messages. This class has no constructor, and the methods can be accessed by using the s_i18n global object.

getMessage(msgKey, config, callback)


Use this method to return a string that contains the data passed in msgKey. The returned data is translated into the user's language. If this string has no localization for the current language, or it does not exist at all, the method returns the msgKey value.

The msgKey parameter contains a string for translation, and the callback parameter is the function that is executed after the server response.

Parameter(s):

NameTypeMandatoryDefault value
msgKeyStringYN
configObjectN{}
callbackFunctionNN

The config parameter is implemented as an object containing three other parameters:

NameTypeMandatoryDefault value
languageStringN''
categoryStringN'app'
paramsObjectN{}

The language parameter can take on the values specified in the Languages (sys_language) table. If it is not specified, the current user's language is used when the method is called.

The category parameter can take on the values specified in the Category field of the Source Message (source_message) table record.

If a message contains any placeholders and the params object has a key similar to the placeholder, the final message will contain its value.

The config parameter is non-mandatory, so you can use a callback function to get translations:

getMessage()
await s_i18n.getMessage('Description', (response) => {console.log(response)});
// Description
caution

This method is asynchronous; for better performance, use the await keyword, as in the code example below.

Return:

TypeDescription
StringThis method returns a message translated into the language specified in the language parameter.

Examples:

getMessage()
if (!s_form.getValue('condition')) {
await s_i18n.getMessage('Specify conditions in "Condition" [condition] field.', (response) => {
s_form.addErrorMessage(response);
});
return false;
}
getMessage()
const serviceNameValue = 'Email';
s_i18n.getMessage('Sorry, The {serviceName} Service Is Temporarily Unavailable', {params: {"serviceName": serviceNameValue}, language: "en", category: "app"}, (response) => console.log(response));

// Sorry, The Email Service Is Temporarily Unavailable

getMessages(msgKey, config, callback)


Use this method to return an array of strings that contains messages in a user language. If a string from the array has no localization for the current language, or it does not exist at all, the method returns its initial value.

The msgKeys parameter passes an array of strings for a translation, and the callback parameter is the function that is executed after the server response.

Parameter(s):

NameTypeMandatoryDefault value
msgKeyStringYN
configObjectN{}
callbackFunctionNN

The config parameter is implemented as an object containing three other parameters:

NameTypeMandatoryDefault value
languageStringN''
categoryN'app'
paramsObjectN{}

The language parameter can take on the values specified in the Languages (sys_language) table. If it is not specified, the current user's language is used when the method is called.

The category parameter can take on the values specified in the Category field of the Source Message (source_message) table record.

If a message contains any placeholders and the params object has a key similar to the placeholder, the final message will contain its value.

caution

This method is asynchronous; for better performance, use the await keyword, as in the code example below.

Return:

TypeDescription
Array of StringsThis method returns an array of strings that can be translated into the language specified in the language parameter.

Examples:

getMessages()
const data = \{};
const btnTitles = ['Accept', 'Cancel'];
await s_i18n.getMessages(btnTitles, (response) => {
[data.acceptBtnTitle, data.cancelBtnTitle] = response;
});
console.log(JSON.stringify(data));

// {"acceptBtnTitle":"Accept","cancelBtnTitle":"Cancel"}
caution

If you use the same placeholders for different messages, the getMessages() method output contains the same values on their place.

getMessages()
window.s_i18n.getMessages(
[
'The date and time should be in {translation}.',
'A user name must contain not less than 2 symbols {translation}.',
],
{ params: { 'translation': 'TEST' } },
);

This method returns the following messages:

  • The date and time should be in TEST.
  • A user name must contain not less than 2 symbols TEST.