Translating text to a specific language: getMessageLang

getMessage relies on the language of the current user. And that’s usually good enough.

But you don’t have the current user when you want to translate notifications.

When you need to translate text to a specific language, but the code is triggered by the system, you can use getMessageLang instead.

[Note: if you are using the multilingual email notifications plugin, getMessage will work fine in notifications and email scripts.]

How does it work?

The syntax is similar to getMessage, but it has an extra parameter to indicate the target language.

gs.getMessageLang(key, language, parms)
Code language: JavaScript (javascript)

This function is partially documented.

I only found a reference in the ServiceNow documentation, on a topic about Virtual Agent conversations.

Nothing about it in the Glidesystem API documentation.

What do we do if ServiceNow blocks it at some point?

Even if the feature is undocumented and it might stop working in a future release, I have used it anyway.

The risk is very low, and you can easily replace it with a custom function that queries the Message [sys_ui_message] table.

To make the life of your future self easier (or whoever is maintaining your code), encapsulate it in a function inside a Script Include and call your own function. If getMessageLang is ever blocked, you just need to change it in one place.

var CustomTranslationUtils = Class.create(); CustomTranslationUtils.getMessageLang = function(key, language, parms){ return gs.getMessageLang(key, language, parms); };
Code language: JavaScript (javascript)