Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CDN Datatables switch language

I am using datatables from datatables.net within a multi-language application. I know how to switch the language of the table with simply passing the language file or customizing the strings by myself.

 "language": {
            "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/lang.json"
        }

But is there a possibility to change the language according to the users browser settings?

like image 606
greenhoorn Avatar asked Jul 07 '15 06:07

greenhoorn


People also ask

Is DataTables an API?

DataTables has an extensive API which can be used to access the data contained in a table and otherwise manipulate the table after the table initialisation has completed. The DataTables API is designed to reflect the structure of the data in the table and how you will typically interact with the table through the API.

How to set language in jQuery DataTable?

u can try other language "oLanguage": { "sUrl": "//cdn.datatables.net/plug-ins/1.10.7/i18n/<LANGUAGE>. json" } a open datatables.net/plug-ins/i18n and look translation is click ur language later take CDN links :) This does not seem to work any more in 1.10.

What is better than DataTables?

The best alternative is jQuery Dynatable. It's not free, so if you're looking for a free alternative, you could try List. js or Webix DataTable. Other great apps like DataTables are Frappe DataTable, Dash DataTable, wpDataTables and Essential JS 2 for JavaScript by Syncfusion.


1 Answers

Unfortunetaly, the langauge packages are named by language names, not by language codes :

//cdn.datatables.net/plug-ins/1.10.7/i18n/Finnish.json
//cdn.datatables.net/plug-ins/1.10.7/i18n/French.json

etc. So you must built a map that translates language codes to language names :

var langMap = {
   'en' : 'English',
   'da' : 'Danish',
   'se' : 'Swedish'
   //etc, the languages you want to support
}

Now you can pass the correct language package URL to dataTables that corresponds to the current browser language :

function getLanguage() {
    var lang = navigator.language || navigator.userLanguage; 
    return '//cdn.datatables.net/plug-ins/1.10.7/i18n/'+langMap[lang]+'.json'
}

var table = $('#example').DataTable({
    language : {
        url: getLanguage()
    }
});

demo -> http://jsfiddle.net/3er6f4w6/

like image 196
davidkonrad Avatar answered Sep 21 '22 08:09

davidkonrad