Change Site Language with JSOM
Hi,
Here is code which can change site language with JSOM :
<script type="text/javascript" charset="utf8" src="/Style%20Library/SPS/jquery-1.11.3.min.js"></script>
<select id='LangSelect' onchange="SetLanguage();"></select>
<script type="text/javascript">
$( document ).ready(function() {
LoadLanguages();
setTimeout(GetUpdatedLangulage, 2000);
});
function LoadLanguages()
{
var call = $.ajax({
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/Web/Lists/GetByTitle('Languages')/items?" + "$select=Title,Code,ID&$top=5000",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function (data, textStatus, jqXHR) {
var option = '';
for (var i=0;i<data.d.results.length;i++){
option += '<option value="'+ data.d.results[i].Code + '">' + data.d.results[i].Title + '</option>';
}
$('#LangSelect').append(option);
});
}
function SetLanguageInRegionalSettings()
{
var context = SP.ClientContext.get_current();
var web = context.get_web();
var regionalSettings = web.get_regionalSettings();
context.load(regionalSettings)
context.executeQueryAsync(function () {
var sel = document.getElementById('LangSelect');
console.log( sel.value );
regionalSettings.set_localeId(sel.value); //English (UK)
regionalSettings.update();
context.executeQueryAsync(function () {
console.log("success");
}, function (sender, args) {
console.log(args.get_message());
});
}, function (sender, args) {
console.log(args.get_message());
});
}
function GetUpdatedLangulage()
{
var context = SP.ClientContext.get_current();
var web = context.get_web();
var regionalSettings = web.get_regionalSettings();
context.load(regionalSettings, "LocaleId");
context.executeQueryAsync(function () {
//Locale Id
console.log(regionalSettings.get_localeId()); //2057
var sel = document.getElementById('LangSelect');
sel.value = regionalSettings.get_localeId();
}, function (sender, args) {
console.log(args.get_message());
});
}
function SetLanguage()
{
var context = SP.ClientContext.get_current();
var web = context.get_web();
//This is important. Wont work otherwise.
web.set_isMultilingual(true);
var sel = document.getElementById('LangSelect');
console.log( sel.value );
web.addSupportedUILanguage(sel.value); //Dutch
//The full list of SharePoint 2013 and SharePoint Online LCIDs is here:
//https://technet.microsoft.com/en-us/library/ff463597.aspx
web.update();
context.executeQueryAsync(function(){
console.log("Language Added");
SetLanguageInRegionalSettings();
},function(sender,args){
console.log(args.get_message());
SetLanguageInRegionalSettings();
});
}
</script>
Here is code which can change site language with JSOM :
<script type="text/javascript" charset="utf8" src="/Style%20Library/SPS/jquery-1.11.3.min.js"></script>
<select id='LangSelect' onchange="SetLanguage();"></select>
<script type="text/javascript">
$( document ).ready(function() {
LoadLanguages();
setTimeout(GetUpdatedLangulage, 2000);
});
function LoadLanguages()
{
var call = $.ajax({
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/Web/Lists/GetByTitle('Languages')/items?" + "$select=Title,Code,ID&$top=5000",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function (data, textStatus, jqXHR) {
var option = '';
for (var i=0;i<data.d.results.length;i++){
option += '<option value="'+ data.d.results[i].Code + '">' + data.d.results[i].Title + '</option>';
}
$('#LangSelect').append(option);
});
}
function SetLanguageInRegionalSettings()
{
var context = SP.ClientContext.get_current();
var web = context.get_web();
var regionalSettings = web.get_regionalSettings();
context.load(regionalSettings)
context.executeQueryAsync(function () {
var sel = document.getElementById('LangSelect');
console.log( sel.value );
regionalSettings.set_localeId(sel.value); //English (UK)
regionalSettings.update();
context.executeQueryAsync(function () {
console.log("success");
}, function (sender, args) {
console.log(args.get_message());
});
}, function (sender, args) {
console.log(args.get_message());
});
}
function GetUpdatedLangulage()
{
var context = SP.ClientContext.get_current();
var web = context.get_web();
var regionalSettings = web.get_regionalSettings();
context.load(regionalSettings, "LocaleId");
context.executeQueryAsync(function () {
//Locale Id
console.log(regionalSettings.get_localeId()); //2057
var sel = document.getElementById('LangSelect');
sel.value = regionalSettings.get_localeId();
}, function (sender, args) {
console.log(args.get_message());
});
}
function SetLanguage()
{
var context = SP.ClientContext.get_current();
var web = context.get_web();
//This is important. Wont work otherwise.
web.set_isMultilingual(true);
var sel = document.getElementById('LangSelect');
console.log( sel.value );
web.addSupportedUILanguage(sel.value); //Dutch
//The full list of SharePoint 2013 and SharePoint Online LCIDs is here:
//https://technet.microsoft.com/en-us/library/ff463597.aspx
web.update();
context.executeQueryAsync(function(){
console.log("Language Added");
SetLanguageInRegionalSettings();
},function(sender,args){
console.log(args.get_message());
SetLanguageInRegionalSettings();
});
}
</script>
Comments