6

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?

1 Answer 1

10

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. So obvious now you said it, why didn't I get it.. I was only searching for some built-in feature.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.