I store, in a session variable, which language does user wants to translate but I don\'t know to pass it DataTables
I found this explanation on the datatables websit
for Arabic language
var table = $('#my_table')
.DataTable({
"columns":{//......}
"language":
{
"sProcessing": "جارٍ التحميل...",
"sLengthMenu": "أظهر _MENU_ مدخلات",
"sZeroRecords": "لم يعثر على أية سجلات",
"sInfo": "إظهار _START_ إلى _END_ من أصل _TOTAL_ مدخل",
"sInfoEmpty": "يعرض 0 إلى 0 من أصل 0 سجل",
"sInfoFiltered": "(منتقاة من مجموع _MAX_ مُدخل)",
"sInfoPostFix": "",
"sSearch": "ابحث:",
"sUrl": "",
"oPaginate": {
"sFirst": "الأول",
"sPrevious": "السابق",
"sNext": "التالي",
"sLast": "الأخير"
}
}
});
Ref: https://datatables.net/plug-ins/i18n/Arabic
Author: Ossama Khayat
French translations:
$('#my_table').DataTable({
"language": {
"sProcessing": "Traitement en cours ...",
"sLengthMenu": "Afficher _MENU_ lignes",
"sZeroRecords": "Aucun résultat trouvé",
"sEmptyTable": "Aucune donnée disponible",
"sInfo": "Lignes _START_ à _END_ sur _TOTAL_",
"sInfoEmpty": "Aucune ligne affichée",
"sInfoFiltered": "(Filtrer un maximum de_MAX_)",
"sInfoPostFix": "",
"sSearch": "Chercher:",
"sUrl": "",
"sInfoThousands": ",",
"sLoadingRecords": "Chargement...",
"oPaginate": {
"sFirst": "Premier", "sLast": "Dernier", "sNext": "Suivant", "sPrevious": "Précédent"
},
"oAria": {
"sSortAscending": ": Trier par ordre croissant", "sSortDescending": ": Trier par ordre décroissant"
}
}
});
});
You have to either create a language file and then set it using :
"oLanguage": {
"sUrl": "media/language/your_file.txt"
}
Im not sure what server language you are using but something like this would work in PHP :
"oLanguage": {
"sUrl": "media/language/custom_lang_<?php echo $language ?>.txt"
}
Where language
matches the file name for a specific language.
or change individual settings :
"oLanguage": {
"sLengthMenu": "Display _MENU_ records per page",
"sZeroRecords": "Nothing found - sorry",
"sInfo": "Showing _START_ to _END_ of _TOTAL_ records",
"sInfoEmpty": "Showing 0 to 0 of 0 records",
"sInfoFiltered": "(filtered from _MAX_ total records)"
}
For more details read this : http://datatables.net/plug-ins/i18n
Tradução para Português Brasil
$('#table_id').DataTable({
"language": {
"sProcessing": "Procesando...",
"sLengthMenu": "Exibir _MENU_ registros por página",
"sZeroRecords": "Nenhum resultado encontrado",
"sEmptyTable": "Nenhum resultado encontrado",
"sInfo": "Exibindo do _START_ até _END_ de um total de _TOTAL_ registros",
"sInfoEmpty": "Exibindo do 0 até 0 de um total de 0 registros",
"sInfoFiltered": "(Filtrado de um total de _MAX_ registros)",
"sInfoPostFix": "",
"sSearch": "Buscar:",
"sUrl": "",
"sInfoThousands": ",",
"sLoadingRecords": "Cargando...",
"oPaginate": {
"sFirst": "Primero",
"sLast": "Último",
"sNext": "Próximo",
"sPrevious": "Anterior"
},
"oAria": {
"sSortAscending": ": Ativar para ordenar a columna de maneira ascendente",
"sSortDescending": ": Ativar para ordenar a columna de maneira descendente"
}
}
});
It is indeed
language: {
url: '//URL_TO_CDN'
}
The problem is not all of the DataTables (As of this writing) are valid JSON. The Traditional Chinese file for instance is one of them.
To get around this I wrote the following code in JavaScript:
var dataTableLanguages = {
'es': '//cdn.datatables.net/plug-ins/1.10.21/i18n/Spanish.json',
'fr': '//cdn.datatables.net/plug-ins/1.10.21/i18n/French.json',
'ar': '//cdn.datatables.net/plug-ins/1.10.21/i18n/Arabic.json',
'zh-TW': {
"processing": "處理中...",
"loadingRecords": "載入中...",
"lengthMenu": "顯示 _MENU_ 項結果",
"zeroRecords": "沒有符合的結果",
"info": "顯示第 _START_ 至 _END_ 項結果,共 _TOTAL_ 項",
"infoEmpty": "顯示第 0 至 0 項結果,共 0 項",
"infoFiltered": "(從 _MAX_ 項結果中過濾)",
"infoPostFix": "",
"search": "搜尋:",
"paginate": {
"first": "第一頁",
"previous": "上一頁",
"next": "下一頁",
"last": "最後一頁"
},
"aria": {
"sortAscending": ": 升冪排列",
"sortDescending": ": 降冪排列"
}
}
};
var language = dataTableLanguages[$('html').attr('lang')];
var opts = {...};
if (language) {
if (typeof language === 'string') {
opts.language = {
url: language
};
} else {
opts.language = language;
}
}
Now use the opts as option object for data table like
$('#list-table').DataTable(opts)