How to use gs.getMessage() to get the string translated?

hyperjam
Giga Contributor

var ret = ' ';

....

....

if(counter_app == 0){

  switch(counter_level){

  case 0:

  ret += "First level of approval "; // <------   how to get such string translated?

  break;

....

  }

  if     (when.indexOf('always') == -1)

                ret += '\(Depends on selected options)\: '; <--------- how to get such string translated too?

else ...

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Sorry Anna,


I thought you were doing a client script.



For server side scripts (business rules, UI actions, etc) there's no Messages field and you use the gs.getMessage() call.



For best results, go to System UI> Messages and create your translated messages. For example,



key=first_level_of_approval, Message=First level of approval



Now in your script...



ret += gs.getMessage('first_level_of_approval');



This may also be helpful: gs.getMessage() - The Second Parameter


View solution in original post

11 REPLIES 11

Chuck Tomasi
Tera Patron

Hi Anna,



For best results, go to System UI> Messages and create your translated messages. For example,



key=first_level_of_approval, Message=First level of approval



Then, in the client script, add this to the Messages field



first_level_of_approval



This tells the client script to retrieve the translated message BEFORE it needs it for better performance.



Now in your script...



ret += getMessage('first_level_of_approval');



Lather, rinse, repeat for other messages you want to translate.



FYI - the server counterpart is gs.getMessage()



Client Scripts - ServiceNow Wiki


Hey Chuck, what it the variant to use on the service portal? Because getMessage() is not supported on the portal and does not return the translation... by design... But there should be some way to translate messages right?



Here is the thread I created


getMessage on Service Portal


You can do this in the server script and set information in the data object that is available to the client and HTML.



Example:



data.more_info_reqd = gs.getMessage('More information required');



FWIW, I tend to load up my messages at the top of the server script for readability rather than embed them inline


Hi Chuck,

I have a same requirement as above:

I have created messages in system localization for all translations(screen shot attached) and using those messages through getMessage() method in catalog client scripts(screen shot attached).

 

Can you please help me figure out the solution