I need to get the name of the current UI language in a javascript file. I know the following trick:
SP.Res.lcid
But that gets the the LCID. What I really need is the two-letter iso name (like 'en', 'fr', etc)
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityI need to get the name of the current UI language in a javascript file. I know the following trick:
SP.Res.lcid
But that gets the the LCID. What I really need is the two-letter iso name (like 'en', 'fr', etc)
You can create a JS object and fetch the values using the LCID:
var CultureNames = { 1033:"en", 1036:"fr" };//Store it in a utility file somewhere.
var currentCultureName = CultureNames[SP.Res.lcid]; //will return 'en' if current culture is english.
You can also do:
Sys.CultureInfo.CurrentCulture.name
Sys.CultureInfo.CurrentCulture
is the server CurrentUICulture in JSON.
This is created by the page's ScriptManager
and to it be available you need to set its property EnableScriptGlobalization
to true
.
I think this is enabled by default in SharePoint.
I created a JavaScript library to handle your requirement (returns the culture name from a decimal lcid value)
The mapping comes from this page: National Language Support (NLS) API Reference (Windows 7)
Practically inside the library there is JSON object with the values and two methods to get them.
The Culture name is the full one (for example en-US for 1033) you can slice the string if you need only the first two letters.
You can find more information on my blog:
In SharePoint 2013 JSOM API you could utilize SP.ServerSettings.getGlobalInstalledLanguages Method to retrieve all the installed languages, like this:
function getInstalledLanguages(success,error)
{
var ctx = new SP.ClientContext.get_current();
var languages = SP.ServerSettings.getGlobalInstalledLanguages(ctx,15);
ctx.executeQueryAsync(
function()
{
success(languages);
},
error);
}
and then find the corresponding language by lcid
:
getInstalledLanguages(
function(languages)
{
var lcid = parseInt(SP.Res.lcid); //get current locale
var language = languages.filter(function(l) {
return l.get_lcid() === lcid;
})[0];
var langTag = language.get_languageTag();
var langCode = langTag.split('-')[0];
console.log(langCode);
},
function(sender,args)
{
console.log(args.get_message());
});