0

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?

jomskris
  • 293
  • 6
  • 17
  • `async: false` requests are deprecated -> [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Andreas Oct 26 '20 at 10:53

2 Answers2

0

As long as you aren't supporting should-be-dead browsers (i.e. IE), and if it is part of the query string, you can use URLSearchParams for this.

const qs = window.location.search;
const params = new URLSearchParams(qs);
const lang = params.get('lang');

If it's part of the url itself, then you'll have to parse your url. You can use the window.location.pathname to get your url path without the domain. Say it looks like this: https://my.domain.com/some/path/en

// remove prepended '/', then break it up
const pathbits = window.location.pathname.slice(1).split('/');
const lang = pathbits[2];
Steve -Cutter- Blades
  • 5,057
  • 2
  • 26
  • 40
0

You can use this function parse_query_string() from this answer https://stackoverflow.com/a/979995/6426512 to get your parameters

then do some simple logic like this

var query_string = window.location.search.substring(1);
var lang = parse_query_string(query_string);

if ( lang==='es') {
         //do something;
    }
else{

    //do something
}

But note this is for newer browsers like the referenced answer says. For older browsers (including Internet Explorer), you can use https://github.com/ungap/url-search-params or the code from the original version of this answer that predates URL:

Alvin Moyo
  • 200
  • 1
  • 9