$.fn.dataTable.settings[0].oLanguage.sLengthMenu = "Show _MENU_ records"; not working

$.fn.dataTable.settings[0].oLanguage.sLengthMenu = "Show _MENU_ records"; not working

kaluosikaluosi Posts: 19Questions: 6Answers: 0

I have to change language from Spanish to English and vice versa. As datatables have not a clear API to change label language, I found this other method to change the language of the labels from Spanish to English:

if (lengua === 'en_BR') {
        $.fn.dataTable.settings[0].oLanguage.sDecimal = ".";
        $.fn.dataTable.settings[0].oLanguage.sThousands = ",";
        $.fn.dataTable.settings[0].oLanguage.sProcessing = "Processing...";
        $.fn.dataTable.settings[0].oLanguage.sLengthMenu = "Show _MENU_ records";
        $.fn.dataTable.settings[0].oLanguage.sZeroRecords = "No results found";
        $.fn.dataTable.settings[0].oLanguage.sEmptyTable = "No data available in this table";
        $.fn.dataTable.settings[0].oLanguage.sInfo = "Showing records from _START_ to _END_ from a total of _TOTAL_ records";
        $.fn.dataTable.settings[0].oLanguage.sInfoEmpty = "Showing records from 0 to 0 from a total fo 0 records";
        $.fn.dataTable.settings[0].oLanguage.sInfoFiltered = "(filtered from a total of _MAX_ records)";
        $.fn.dataTable.settings[0].oLanguage.sInfoPostFix = "";
        $.fn.dataTable.settings[0].oLanguage.sSearch = "Search:";
        $.fn.dataTable.settings[0].oLanguage.sUrl ="";
        $.fn.dataTable.settings[0].oLanguage.sInfoThousands = ",";
        $.fn.dataTable.settings[0].oLanguage.sLoadingRecords = "Loading...";
        $.fn.dataTable.settings[0].oLanguage.oPaginate.sFirst = "First page";
        $.fn.dataTable.settings[0].oLanguage.oPaginate.sLast = "Last page";
        $.fn.dataTable.settings[0].oLanguage.oPaginate.sNext = "Next Page";
        $.fn.dataTable.settings[0].oLanguage.oPaginate.sPrevious = "Previous page";
        $.fn.dataTable.settings[0].oLanguage.oAria.sSortAscending = ": Activate to sort the column in ascending order";
        $.fn.dataTable.settings[0].oLanguage.oAria.sSortDescending = ": Activate to sort the column in descending order";
   }

Most of them are working properly but:

$.fn.dataTable.settings[0].oLanguage.sLengthMenu = "Show _MENU_ records";

does not work

Any idea??

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    You should use language.lengthMenu for that - see here. It's best to avoid going into the settings as it's considered private and may change in the future.

    Colin

  • kaluosikaluosi Posts: 19Questions: 6Answers: 0

    I mean, change the language once the table is loaded

  • kaluosikaluosi Posts: 19Questions: 6Answers: 0

    Thank you Colin. Your answer is related with the settings of datatables when it is called in javascript the first tiem. The problem is that if you want to change the interface language once the datatable is load, this kind of setting does no work.

    For example, if you call datatables like the example you of live.datatables.net/citodoza/1/edithttp://, then you can not load again the datatables with differentes settings affecting only the language, for example.

    For example, if I have loaded datatables with

     var table = $('#example').DataTable({
        language: { lengthMenu: 'XXX Display _MENU_ records'}
      });
    

    You can not change the length menu once the page is loaded with:

    table.language.lengthMenu: 'YYY Mostrar _MENU_ registros'
    

    Or could you??

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Unfortunately, I don't think you can, you'll need to reload the table with the new values. If you add destroy to the table, it will remove the old one, something like this.

    Hope that does the trick,

    Colin

  • kaluosikaluosi Posts: 19Questions: 6Answers: 0

    At the end I have found a solution to my problem. It is just to separate in to different objects the settings for the datatable. One object for the general settings, other object for the Spanish UI settings and another one for the English UI settings.
    Then I check the language of the UI and merge together the object of the general settings with the objects of one of the language or the other. Just then I create the datatable object with the merged objects

    let general_settings = {
         // general settings ....
    }
    
    let spanish_settings {
    "language": {
               "sDecimal": ",",
               "sThousands": ".",
               "sProcessing":     "Procesando...",
               "sLengthMenu":     "Mostrar _MENU_ fichas",
               "sZeroRecords":    "No se encontraron resultados",
               ......
        }
    };
    
    let english_settings {
    "language": {
                "sDecimal": ".",
                "sThousands": ",",
                "sProcessing":     "Processing...",
                "sLengthMenu":     "Show _MENU_ records",
                "sZeroRecords":    "No results found",
                .....
       }
    };
    
    if (lengua === 'en_BR') {
           $.extend(general_settings, english_settings);
       } else {
           $.extend(general_settings, espanish_settings);
       }
    
    // then create the table
    let table = $('#table').DataTable(general_settings);
    

    Hope this can be helpful for others. I think this approach can be used for other things as well

This discussion has been closed.