AnveshKumar M
Tera Sage
Tera Sage

Hi, everyone!

 

I wanted to share my experience with using two common ServiceNow functions for retrieving translated messages: gs.getMessage() and gs.getMessageLang().

 

These functions are often used to display language-specific messages to users.

 

gs.getMessage() is the default choice, and it dynamically adapts to the language preference of the current user. For example you can use it in your portal widget to display translated text.

 

This method retrieves translated messages from sys_ui_message (Message) table.

 

Method Signature: getMessage(String messageID, Object args)

 

Parameters:

Name Type Description
messageID String Message identifier (Key field of the Message [sys_ui_message] table) 
args Object Optional. List of strings or other values defined by java.text.MessageFormat that replace the variables within the specified message.
Note: The passed in values are not translated. They are inserted as is in the message.

 

Returns: The translated message from Message table (String)

 

Example:

// Retrieving the translated message for the "Hello" text
var hello_text = gs.getMessage('Hello');

// Retrieving the translated message for the "Hello" text with variables replaced. Replaced text will not be translated
var usrName = "Anvesh";
var greeting_text = gs.getMessage("Hello {0},", [usrName]);

 

If the Message has Tick character, this may cause issues in the script, to overcome this you can use gs.getMessageS() method, which escapes the tick (') character, but this is not available for Scoped Apps.

 

For more information on gs.getMessage() refer the Developer Reference at https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server_legacy/c_GlideSystemAPI#r_G...

 

For more information on gs.getMessageS() refer the Developer Reference at https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server_legacy/c_GlideSystemAPI#r_G...

 

gs.getMessageLang(), on the other hand, is more specific. It always returns the message in the specified language, regardless of the user's language preference. This makes it useful for notifications, and email scripts, where the language of the recipient is crucial instead of the default system language.

 

Method Signature: getMessageLang(String message, String language, Array args)

 

Parameters:

Name Type Description
message String Text to translate 
language String Language code, to which text needs to be translated
args Object Optional. List of strings or other values defined by java.text.MessageFormat that replace the variables within the specified message.
Note: The passed in values are not translated. They are inserted as is in the message.

 

Returns: Translated message

 

Example:

// Sending an email notification in Finnish (Suomi)
var emailSubject = gs.getMessageLang('Email Subject', 'fi');
var emailBody = gs.getMessageLang('Email Body', 'fi');

 

Choosing the Right Tool for the Linguistic Task

The decision between gs.getMessage() and gs.getMessageLang() hinges on the specific context of the script. For text that will be displayed to the logged in user, gs.getMessage() takes the lead, providing a seamless language-adaptive experience. Conversely, for notifications, and email scripts, where language specificity is paramount, gs.getMessageLang() stands as the unwavering champion.

 

A Comparative Glimpse

 

Feature gs.getMessage() gs.getMessageLang()
Language Adaptability Yes No
Language Specificity No Yes
Suitable for Text in UI that is rendered from Server Side Script Yes No
Suitable for Notifications No Yes
Suitable for Email Scripts No Yes
Available in Global Scope Yes Yes
Available in Scoped Applications Yes Yes

 

So, which function should you use? It depends on the specific context of your script. For displaying translated text in UI, use gs.getMessage(). For notifications, and email scripts, use gs.getMessageLang().

 

For more info on gs.getMessageLang() refer developer reference link at https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server_legacy/c_GlideSystemAPI#GS-...

 

Note:  At least one language specific I18N plugin should be activated. Refer the following link for documentation.

https://docs.servicenow.com/csh?topicname=t_ActivateALanguage.html&version=latest

 

I hope this helps! If you have any questions, please don't hesitate to ask.

Comments
CezaryBasta
Tera Guru

This content is simply misleading, and that's the best I can say.

1. None of the mentioned functions can be used in user-facing scripts, they are both server-side methods.

2. You didn't mention that gs.getMessageLang() is a scoped method.

3. You forgot to mention gs.getMessageS().

4. You don't explain from where the translated mesages come from and how they can be parametrized

5. You don't mention the i18n API at all And finally, if you are explaining something, reference the documentation: https://developer.servicenow.com/dev.do#!/reference/api/utah/server/no-namespace/c_GlideSystemScoped... https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/c_GlideSystemAPI#r_GS-get...

AnveshKumar M
Tera Sage
Tera Sage

Hello @CezaryBasta 

Thank you very much for your constructive comments, I have updated the article with all your suggestions.

 

My intention for using "user-facing scripts" is this can be used for displaying translated text in UI, anyway I replaced that word as it may mislead.

And, gs.getMessageLang() works both in Global and Scoped applications.

 

Thanks,

Anvesh

Version history
Last update:
‎11-24-2023 08:03 AM
Updated by:
Contributors