Notification variable not getting translated
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2022 08:00 AM
Hi,
For translation of notifications, we have created notification in each language(french and english). For one of the french notifications, there are variables added in the message body. This notification is created in the sc_request table.
I have changed the language of the recipient to french in the user table and as well as changed the language in profile to french. Look at the image below, the ui is now in french. The field impact's value is translated here.
However when the notification is triggered, you can see the word, 3 -Medium is not translated. The same word is translated as 3 – Modérée in the above image, which is taken from the sc_request table from which the variable is used in the notification record.
How to fix this and translate that variable's value. The translated value is already available in the sys_choice table, but why it does not appear in the notification still remains unclear (language is set to french both in the user table and in the preferences table) .Any workaround/help is appreciated.
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2022 01:03 AM
What you might do is to create a "special" notification e-mail script that just loads the recipient user's language and places it into a global variable:
var u_translationLanguageCode = (function runMailScript (current, template, email, email_action, event, sys_user_uniqueValue) {
var sys_user = new GlideRecord('sys_user');
return sys_user.get(sys_user_uniqueValue) ? sys_user.preferred_language + '' : gs.getSession().getLanguage();
})(current, template, email, email_action, event, '<The sys_id of the recipient user>');
Of course this notification e-mail script must be included into a notification as the 1st item in the Message body. Once that is done, the variable will be available for subsequent notification e-mail scripts:
var getreq = new GlideRecord('sc_request');
getreq.addQuery('sys_id',current.sys_id);
getreq.query();
if(getreq.next())
{
var impactis = getreq.impact;
var getimpacttranslated = new GlideRecord('sys_choice');
getimpacttranslated.addQuery('element','impact');
getimpacttranslated.addQuery('name','task');
getimpacttranslated.addQuery('language', u_translationLanguageCode);
gs.info("translation test " + u_translationLanguageCode);
getimpacttranslated.addQuery('value',impactis);
getimpacttranslated.query();
if(getimpacttranslated.next())
{
gs.info("translation test " + getimpacttranslated.label);
template.print(getimpacttranslated.label);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2022 01:43 AM
Can you explain what the first script does? So both the snippets should be in one email script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2022 02:07 AM
The two scripts should be in separate e-mail scripts. To clear up things, even if several e-mail scripts are included and executed while an e-mail is generated, those all share a single session and global object. That is why a global variable set by one e-mail script, will be available to all e-mail scripts executed after it. Also to clear things up, those variables that are defined outside the default IIFE (
(function runMailScript (current, template, email, email_action, event) {
})(current, template, email, email_action, event);
) all become global variables.
So if one writes:
(function runMailScript (current, template, email, email_action, event) {
var i = 0;
})(current, template, email, email_action, event);
i
will not be a global variable, because it has been defined inside the IIFE. It will only be available inside the IIFE. However if one writes:
var i = 0;
(function runMailScript (current, template, email, email_action, event) {
})(current, template, email, email_action, event);
i
will be a global variable, because it has been defined outside the IIFE. It will be usable not only in the "current" e-mail script, but in all e-mail scripts that will be executed after it. Actually it will be available in all e-mail scripts, just that in e-mail scripts executed before it, the variable will be undefined
.
Back to the e-mail script to get the current e-mail's language, it is just an example, the important bit is that it sets global variable u_translationLanguageCode
. It loads a user record and reads and returns the preferred_language
field, or if the user record could not be loaded, it will fall back to the session language. You will need to write your own script to determine the correct language, whether in a reusable e-mail script or in all e-mail scripts. B.t.w. you could also move the code to determine language into a Script Include and later call it in all e-mail scripts that need it, but in that case you will be calling the same code multiple times for no reason. But it might be safer.
But how exactly would you know which user to load, I don't know, it depends how the recipient is set. Is it the Requested for user? Is it someone in the Watch list field? Or an arbitrary user? Only you can tell :-). But the bottom line is, you need to find that use in the user database and put the value in field preferred_language
into a global variable (u_translationLanguageCode
). Of course, you can name your global variable anything you'd like - but you should use the u_ prefix to make it less likely to collide with other SN global variables. I suppose I could be more precise in what script you need to right to set the global language variable, but you need to tell us how do you determine the recipient here.
Finally the script that I posted above could be written as below too:
var u_recipientUniqueValue = u_getRecipientUniqueValue();
var u_translationLanguageCode = u_getTranslationLanguageCode(u_recipientUniqueValue);
function u_getRecipientUniqueValue() {
// This needs to return the recipient user's sys_id
}
function u_getTranslationLanguageCode(sys_user_uniqueValue) {
var sys_user = new GlideRecord('sys_user');
return sys_user.get(sys_user_uniqueValue) ? sys_user.preferred_language + '' : gs.getSession().getLanguage();
}
Just that here I no longer employ an IIFE, thus I am polluting the global scope with unnecessary variables like u_recipientUniqueValue
, u_getRecipientUniqueValue
and u_getTranslationLanguageCode
which don't need to be there.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2022 07:19 AM
I have used exactly the script you gave. What should be used instead of ?
<The sys_id of the recipient user>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2022 08:14 AM
For that, can you show what is the "Who receives" configuration of your notification?