Please help any JavaScript functions to retrieve the browser language code since there is potential missing from either service now user table or current session language set.
gs.getSession().getLanguage() will retrieve current session pre-defined language but I want to get browser langugage/language code as kind of backup.
navigator.language || navigator.userLanguage seems like not working since it will show error of navigator is not defined.
also tried to create a new class of script include, but seems like not working either as following:
var BrowserLocaleUtil = Class.create();
BrowserLocaleUtil.prototype = {
initialize: function() {},
getBrowserLocale: function(request) {
var acceptLanguage = request.getHeader('Accept-Language');
if (acceptLanguage) {
var languageTag = acceptLanguage.split(',')[0];
return languageTag;
} else {
return 'en';
}
},
type: 'BrowserLocaleUtil'
};
and call in other script var browserLocaleUtil = new BrowserLocaleUtil();
var browserLocale = browserLocaleUtil.getBrowserLocale(current.request);
Your script should work with following line:
var acceptLanguage = GlideTransaction.get().getRequest().getHeader("Accept-Language");
This method is mentioned in thread below: https://www.servicenow.com/community/developer-articles/howto-automatic-server-side-language-selection-for-the-login/ta-p/2301146 There is no documentation for GlideTransaction so it's requires some testing. For client side scripts you can use:
navigator.language