greenhoorn
greenhoorn

Reputation: 1561

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?

Upvotes: 6

Views: 2534

Answers (1)

davidkonrad
davidkonrad

Reputation: 85538

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/

Upvotes: 10

Related Questions