Return a message in a specific language, gs.getMes... - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Hi there,

 

When scripting hardcoded messages, a best practice is to have maintainability and internationalization in mind. An often-used method for hardcoded messages would be to apply getMessage (Client Side) or gs.getMessage (Server Side).

 

Though what if you want to translate a hardcoded message (Server Side) into a specific language or into the logged-in user's language when the script is executed as system, or in background? There's an undocumented method for that:

 

gs.getMessageLang()


gs.getMessage()

You probably are familiar with gs.getMessage() and maybe gs.getMessageS() <-- No this is not a typo. This method is well documented. If the specified message exists in the UI Messages table [sys_ui_message] for the current language, the method returns the translated message. If the specified message does not exist for the current language, the method returns the English version of the message. In most cases, this will be sufficient.


gs.getMessageLang()

There will be situations where gs.getMessage() is not sufficient. When you would like to have the message always displayed in a certain language. Or where the script is executed as system, in background, etcetera and simply the English version of the message is always returned.

 

A good example, Notifications, and Mail Scripts. When a Notification is triggered by the notification conditions, the Notification is generated by system. Therefore if you would have applied gs.getMessage() in a mail script, it would always return the English version of the message.


Or what if you - for whatever reason - always want a message to be returned in a specific language?

Undocumented, though used out-of-the-box on multiple places: gs.getMessageLang(). The gs.getMessageLang() method checks the UI Message table [sys_ui_message] for the translated version of the text in the language provided as a second parameter. This could be a hardcoded second parameter, though also a dynamic second parameter.

 

Parameters of gs.getMessageLang():

gs.getMessageLang('message_key', 'language_id');


Usage

So if you are after scripting a message always in a certain language, you could apply something like below, which would always return the Dutch version of the message:

 

gs.getMessageLang('What is it you want to have changed?', 'nl');

 

A different approach could be when you are working with mail scripts, and you are after returning the message in a certain users language (maybe even the e-mail receiver): 

 

var language = current.assigned_to.preferred_language;
var message = gs.getMessageLang('An incident has been assigned to you.', language);

template.print(message);

 

Or when there's a variable involved:

 

var language = current.assigned_to.preferred_language;
var message = gs.getMessageLang('Incident {0} has been assigned to you.', language, [current.number]);

template.print(message);

---


And that's it actually. Hope you like it. If any questions or remarks, let me know!

 

C

If this content helped you, I would appreciate it if you hit bookmark or mark it as helpful.

 

Interested in more Articles, Blogs, Videos, Podcasts, Share projects I shared/participated in?
- Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Kind regards,


Mark Roethof

ServiceNow Technical Platform Architect @ Quint Technology

2x ServiceNow Developer MVP

2x ServiceNow Community MVP

---

LinkedIn

Comments
Tom C1
Tera Expert

This is excellent thank you!

 

What happens in a notification script if it triggers for multiple users, who each prefer a different language. My understanding is that a notification is generated once, and sent to all users. Would this generate the same notification but in their preferred language?

 

 

Mark Roethof
Tera Patron
Tera Patron

That's a situation which unfortunately is still not covered with this. The same email is sent to all those addressees while applying gs.getMessageLang would cover one specific language. At least it covers a language that you can control, though still only one.

Maybe it would be possible to parse the addressees if those are in mail script generated, and use the language of the addressee parsed dynamically. Something to test, though my guess would also be for this probably no go.

Kind regards,
Mark

Sebastian R_
Kilo Sage

I did a short research and "found" the documentation in Virtual Agent Localization.

From Virtual Agent documentation it looks like that the gs.getMessageLang is available since Orlando.

richa83
Mega Contributor

Hi!

This is a nice article. Thankyou!

But I am still struggling to resolve my language translation for 'comments' added.

I am using gs.getMessage('${comments}'), which holds 'Additional comments' and 'time stamp' within. How can I translate this?

Regards

Richa

Kevin Paul
Mega Guru

Any update on sending to multiple users?

GM5
Giga Guru

I had the same scenario, we changed the notification to be triggered by events, then the on the BR who triggers the event, we added the recipients and their language as parameters, so if you have 4 recipients with different languages you will trigger 4 notifications for each recipient so on the mail script side you just get your param where the language code is stored and do your translation. ... or use flow designer's for each to isolate the recipients

Vivi Brasil
Kilo Sage

Excellent article, thank you so much! We have the same scenario here, but in our particular implementation we prefer to send one single email for all the recipients which use the same language, as it avoids to exceed the Activity Stream entries limit (I have commented about this reason on this another community article: Multilingual for a Group Notifications - Watch_list).

Vivi Brasil
Kilo Sage

Excellent article @Mark Roethof, thank you so much! We have the same scenario here, but in our particular implementation we really prefer to send one single email for all the recipients which use the same language (one notification per language), as it avoids to exceed the Activity Stream entries limit (I have commented about this reason on this another community article: Multilingual for a Group Notifications - Watch_list).

Sebastian L
Mega Sage

You can't by doing this, since it is dynamic content. Same goes for short_description, description etc. on task records. 

If you have dynamic translation available you can call that API, or you can call a custom translation API to get string fields translated. gs.getMessage and getMessageLang only works for static translation! 🙂 

 

 

Community Alums
Not applicable

I want to show message on Pop-up as per login users language but I tried with below code but its not working!

 

var myUserObject = gs.getUser();

var lang = myUserObject.getLanguage();

var message = gs.getMessageLang('Testing11', lang);

answer = confirm(message);

 

above code is not working on UIAction 

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Please open a community question for this.

Kind regards,
Mark

Version history
Last update:
‎08-14-2024 11:07 AM
Updated by:
Contributors