Optaining user language in a catalog script with g_user?

jesusemelendezm
Mega Guru

Hi, I need to display a msg if user language is "English". I am working with Var loginLanguage = g_user.getClientData("loginlanguage"); but the language value is undefined? my current language is English. Any help is appreciated.

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

There's a global variable called "g_lang" that you can use.   The value would be "en" for English.



e.g. alert(g_lang) will display "en" in an alert dialog.


View solution in original post

20 REPLIES 20

Thanks Jim!



Which table would I apply the business rule to?


I have tried catalog client scripts, catalog items, and requested items but none of them seem to be working for me.



Cheers,


- James


What is your use case for getting the language?   Looks like it is in the Service Catalog?


Yep, I have a service catalog item that has a field I would like to validate in real time.


When the onchange verifies the data, an alert message appears to say something like "Please only enter numbers".


I need this alert message to be English or French depending on the current user's language.


Its ugly and slow but I have a workaround for now:



in the client script:


LangAlert("hello world","Bonjour Tout Le Monde");



function LangAlert(EnText,FrText){


  var UserSysID = g_user.userID;


  var ga = new GlideAjax('smseAJAXOtherExpense');


  ga.addParam('sysparm_name','getUserLocalizedAlert');


  ga.addParam('sysparm_usersysid',UserSysID);


  ga.addParam('sysparm_entext',EnText);


  ga.addParam('sysparm_frtext',FrText);


  ga.getXML(AjaxAlert);


}



function AjaxAlert(response){


  var alertMessage = response.responseXML.documentElement.getAttribute("answer");


  alert(alertMessage);


}



In a script include:


,


getUserLocalizedAlert: function() {


  var UserSysID = this.getParameter('sysparm_usersysid');


  var EnText = this.getParameter('sysparm_entext');


  var FrText = this.getParameter('sysparm_frtext');


  var gr = new GlideRecord('sys_user');


  if(gr.get(UserSysID)){


    if(gr.preferred_language == 'en'){


      return EnText;


    } else {


      return FrText;


    }


  }


}


You should then be able to use "alert(getMessage("u_catalog_please_enter_a_number"));" and have two Messages entries, one for English and one for French.   And to help performance, add the "Messages" field to the Catalog Client Scripts form and add "u_catalog_please_enter_a_number" to that field.   That will pre-load the messages instead of having to make a trip to the server to get the appropriate one.



find_real_file.png