I have a DataTable and I want to change language of the datatable if the user selects an English version of the site I want to translate datatable to English, or Spanish.
So far my code looks like this:
var langMap = {
en: {
path: 'English',
mods: {
sLengthMenu: "Display _MENU_ records per page - custom test"
}
},
es: {
path: 'Spanish',
mods: {
sLengthMenu: "Mostrar _MENU_ registros - algo muy especial..."
}
}
};
function getLanguage() {
var lang = 'es' //$('html').attr('lang');
var result = null;
var path = '//cdn.datatables.net/plug-ins/1.10.13/i18n/';
$.ajax({
async: false,
url: path + langMap[lang].path + '.json',
success: function(obj) {
result = $.extend({}, obj, langMap[lang].mods)
}
})
return result
}
What I am trying to achieve is this value var lang = 'es'
not be hardcoded so, I want to check if the URL contains /es
or /en
and update that value.
Something like this:
function getLanguage() {
if ( document.location.href.indexOf('/en') > -1 ) {
var lang = 'es';
}
var result = null;
var path = '//cdn.datatables.net/plug-ins/1.10.13/i18n/';
$.ajax({
async: false,
url: path + langMap[lang].path + '.json',
success: function(obj) {
result = $.extend({}, obj, langMap[lang].mods)
}
})
return result
}
Can somebody try to help me with this?