17

Is it possible to get the display language from within ECMAScript? Similar to Get the current UI language from a webpart, I'd like to get the user's display language, however I want to access it with the Client Object Model in JavaScript (http://msdn.microsoft.com/en-us/library/ee538253.aspx). Is it somehow possible to access the System.Threading namespace? Or are there any other possibilities?

Modery
  • 547
  • 2
  • 6
  • 16

4 Answers4

28

You have two easy ways to get the LCID in SP JavaScript

  1. var lcid =_spPageContextInfo.currentLanguage;
  2. var lcid = SP.Res.lcid;

Note that option 1 returns an int (i.e. 1033) and option 2 return a string (i.e. "1053").

Gabriel Smoljar
  • 356
  • 5
  • 20
5

In my case I needed to know the language before the SharePoint javascript libraries are loaded in order to hide elements before they are shown on the screen (not possible with _spBodyOnLoadFunctionNames).

Turns out SharePoint sets the lang attribute of the root HTML element according to the language and as a bonus it's already translated to the "en-us" format.

This is done with a simple call to document.getElementsByTagName('html')[0].getAttribute('lang')

Lars Lynch
  • 396
  • 3
  • 4
3

Yet another ways to determine LCID in SP via JavaScript

1 Global variable g_wsaLCID:

var lcid = g_wsaLCID;

2 Language property of Web client object via CSOM (JavaScript)

function getWebLocale(complete) {

    var context = SP.ClientContext.get_current();
    var web = context.get_web();

    context.load(web);
    context.executeQueryAsync(
                function(sender, args){
                    var lcid = web.get_language(); //returns LCID
                    return complete(lcid); 
                }, 
                function(sender, args){
                    complete(-1);
                });
}      

//Usage
function printWebLanguageSettings(){ 
    getWebLocale(function(lcid){
       console.log(lcid);
    });
}    
SP.SOD.executeOrDelayUntilScriptLoaded(printWebLanguageSettings, 'sp.js');
Vadim Gremyachev
  • 42,498
  • 3
  • 86
  • 167
0

When using the Bing translation, the methods above didn't work (the values don't get updated when the language is changed).

What worked for me was

LanguagePicker.currentLang
evanmcd
  • 131
  • 3