Export datatble in translated language when using a 3rd party Translator.

Export datatble in translated language when using a 3rd party Translator.

chsxechsxe Posts: 8Questions: 3Answers: 0

Hi,

If I am using a 3rd party plugin to translate my page, how can I get the datatable to export the data in the chosen languag e.g If i translate my page to German and I export the datatable to excel, it's exported in English (I would like to have it in the chosen language "German").

Thanks

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,705Questions: 1Answers: 10,102 Site admin

    Does the 3rd party plug-in update the DOM? Or does it do something else? You would need to call rows().invalidate() if it updates the DOM so DataTables can read in the translations (the exported data comes from cache).

    Allan

  • chsxechsxe Posts: 8Questions: 3Answers: 0

    It translates all text in the DOM. It detects and translates content, much like a standard browser translator.

    How would I implement that on Export, say for example inside exportOptions?

    extend: 'pdf',
                        title: 'Title',   
                        orientation: 'landscape',
                        exportOptions: {          
                        **Can invalidate be used here?**
                            columns: [],                       
                        }
    

    Thanks

  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769
    Answer ✓

    Sounds like Allan's suggestion of using rows().invalidate() might be the correct option. You will want to use that after the translation is complete. The API will tell Datatables to re-read the DOM to update its data cache which is what is used for exporting.

    Kevin

  • chsxechsxe Posts: 8Questions: 3Answers: 0

    @kthorngren I have a function that is called when the language is changed, inside it I have done what was suggested. When I Export the table to Excel after I have translated it to another language, it is still exported in English.

    How would i have it Cache the translated table data as it currently sits (translated) based on this code snippet:

    Translator.on("languageChanged", function () {
                table
                    .rows()
                    .invalidate()
                    .draw();
            });
    
  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769

    Without seeing it its hard to say. Can you provide a link to your page or a test case replicating the issue?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Do you get any errors in your browser's console?

    Kevin

  • chsxechsxe Posts: 8Questions: 3Answers: 0

    @kthorngren I apologise, it is working. I just didn't notice because instead of exporting just the visible 10 rows, it had been exporting all of them. The 10 visible rows where at the bottom of my spreadsheet. My next problem is to export only the 10 visible translated rows.

    Thanks

  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769
    Answer ✓

    See if this works:

               exportOptions: {
               columns: ':visible',
                 rows: ':visible'
              }
    

    Kevin

  • chsxechsxe Posts: 8Questions: 3Answers: 0

    @kthorngren To get this working this what needs to happen when the Language changed function is called, only use invalidate('dom'). Do not use draw() as this obscures the table data.

        Translator.on("languageChanged", function (params) {
            table
                .rows()
                .invalidate('dom');                              
        });
    

    In ExportOptions, if you don't specify rows: visible, you will end up with the visible rows translated, and the rest of the data in the default language.

                        exportOptions: {
                            columns: [1, 2, 3, 4, 5, 6],
                            rows: ':visible'
                        }
    

    Thanks for your help.

  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769

    Great, thanks for the update. Glad you got it working.

    Kevin

This discussion has been closed.