How to Change the system language on the basis of user's preferred communication language?

vishal jaiswal
Tera Contributor

Scenario: There is a field "" in sys_user table and one field "Language" in system setting.

Expected solution: Need to change the system language on the basis of user's preferred communication language.

Example: If user's preferred communication language is FI then system language should automatically change to FI.

Please refer the attached pic.

 

15 REPLIES 15

AndyLock
Mega Guru

I think it would be dangerous to change the system setting, as this would affect all users. 

If the user is set to FI then that user should see FI text, assuming the translations are in place. I assume you have the language pack installed? Activate a language | ServiceNow Docs

If you have it installed but you are not OOB with the messages then you will need to create your own translations. Language internationalization support | ServiceNow Docs

The Debug translations (servicenow.com) is a handy property, but it's system-wide - so warn everybody before you turn it on in your sub-prod instance.

 

 

Notifications for different languages are also a bit of a mess. We created our own version of getMessage (findmessage) where we can pass the required language as a parameter. This uses sys_ui_message, one of the OOB translations tables.

Then we created a Script Include to work out which language should be passed (getEmailLanguage). Basically, if all recipients speak the same language then use that one, else use the system default.

Finally, our Email scripts were modified to use the above.

All three elements are posted below.

 

Description:
Returns the language-specific text from the messages table, based on the key and language passed to it.
This allows us to swap gs.getMessage for findMessage, thus allowing templates to be in the caller's own language instead of the system language.

Script:
function findMessage(messageKey, messageLanguage, messageArguments) {
 var message;     
 
 if(messageKey == undefined)
  {
   return "";
  }

 if(messageLanguage != undefined)
  {
    //If messageLanguage was passed in do a manual query to get the correct translation
    var msg = new GlideRecord('sys_ui_message');
    msg.addQuery('language',messageLanguage);
    msg.addQuery('key',messageKey);
    msg.query();

    if(msg.next())
    {
       message = msg.message;
    }
  }
     
 if(message == undefined || messageLanguage == undefined)
  {
            //If a messageLanguage was not passed in or a message not found in the query above, use the system default language by using getMessage.
            message = gs.getMessage(messageKey);
  }    

 if(message == undefined)
  {
   //If message is still undefined, default to the messageKey
   message = messageKey;
  }

 
 //Check for arguments passed in
 if(messageArguments != undefined)
  {
   //Swap any placeholders such as {0}, {1}, etc 
   for (var arg in messageArguments)
    {
     if (messageArguments[arg] != undefined)
      {
       message = message.replace(new RegExp('{'+arg+'}', 'g'), messageArguments[arg]);    
      }    
    }   
  } 
 
 return message;
 
}

 

 

Script Include

Description
Determines the best language to use for an email based on the recipient list, caller, and assigned to user.

Script:
function getEmailLanguage(current, email_action, event){
 
 //Defaulting to English
 var lang = 'en';
 
 //Get the full user records of all the recipients
 var recipients = getEmailRecipientUsers(current, email_action, event);
 
 if (recipients != undefined){
  switch (recipients.length){
   case undefined: //Sanity check
    break; 
   case 0: //Do nothing if there are no recipients
    break; 
   case 1: //Return user's language if there is only one recipient
    lang = recipients[0].preferred_language; 
    break;
   default: //Return the common language among recipients if available, otherwise return default language
    var use_userLang = true;
    var userLang = recipients[0].preferred_language;       
    for(i=0; i<recipients.length; i++){
     if (userLang != recipients[i].preferred_language)
      {
       //If two languages found, set flag so user language is not used then break out of for loop
       use_userLang = false;
       break;
      }
    }
    if (use_userLang) {lang = userLang;}
  } 
 } 
 
 return lang;
}

 

Description
Example Email script

(function runMailScript(current, template, email, email_action, event) {
 
 var lang, desc, args=[];
 lang = getEmailLanguage(current, email_action, event);   
 
 desc = new ServiceRequestHelper().getVariableValue(current.sys_class_name=='sc_req_item'?current.number:current.document_id.number,'rq_kb_short_description'); 


 args.push(desc);
 
 template.print(applyStandardEmailTextFormatting(findMessage('Request KB Email Body', lang, args))); 

})(current, template, email, email_action, event);