Get logged in user language into script include

Abhijit Das7
Tera Expert

Hi All,

 

I working with script include . I want to get language of the logged in user into script include.

 

I tried this to use in script include but , it is not working :

var language = gs.getUser().getLanguage();

 

please tell me how to get language of the logged in user.

 

Thanks in advance

7 REPLIES 7

Hi @Abhijit Das7 

can you try

var userPref = new GlideRecord("sys_user_preference");
userPref.addQuery("user", gs.getUserID());

userPred.addQuery("name" "user.language");
userPref.query();
if (userPref.next()) {

gs.info(userPref.value);

}

 

If my answer has helped with your question, please mark it as correct and helpful

Regards,

Ravi Chandra.

Manmohan K
Tera Sage

@Abhijit Das7 

 

You can use gs.getSession().getLanguage(). This will give you the language opted by  Logged in user

 

But don't expect the code to return full name of language like English or Arabic. The reason being servicenow stores language preference in terms of language id/code. 

So for English, language code is en and for Arabic, language code is ar

 

Similarly you can find the code for other languages in below link (You can use the mapping in this link for getting full name of language)

http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry

Ravi Chandra_K
Kilo Patron
Kilo Patron

Hi @Abhijit Das7 

you can try the below code:

 

var langUtils = Class.create();
langUtils.prototype = {
initialize: function() {
},
 
getUserLanguage: function() {
var userLanguage = this._getLanguage();
if(userLanguage == "en")
// gs.info(userLanguage);
return true;
},
 
_getLanguage: function() {
return gs.getSession().getLanguage();
},
 
type: 'langUtils'
};
 
please mark the answer as helpful and accept the solution if it helped. 
 
Regards,
Ravi Chandra.