Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Set language globally without external file #650

Closed
Misiu opened this issue Mar 7, 2016 · 12 comments
Closed

Set language globally without external file #650

Misiu opened this issue Mar 7, 2016 · 12 comments
Labels
Milestone

Comments

@Misiu
Copy link

Misiu commented Mar 7, 2016

Hi, first of all thank You for building this awesome piece of code 😄

Now here is problem I'm facing:
I'd like to translate all instances of DataTables, using instructions I found in API section I'm doing this:

.run([
    "DTDefaultOptions", function(DTDefaultOptions) {

//this works fine
        DTDefaultOptions.setLanguage({
            sUrl: "../Content/pl.js"
        });

//this doesn't works
        DTDefaultOptions.setLanguage({
            "processing": "Przetwarzanie...",
            "search": "Szukaj:",
            "lengthMenu": "Pokaż _MENU_ pozycji",
            "info": "Pozycje od _START_ do _END_ z _TOTAL_ łącznie",
            "infoEmpty": "Pozycji 0 z 0 dostępnych",
            "infoFiltered": "(filtrowanie spośród _MAX_ dostępnych pozycji)"
        });
    }
])

First option works fine, but this involves one extra request to server for each instance.
I'd like to avoid it and set language single time, inline.

In run I'm setting language configuration and then inside controller I'm setting configuration for specific instance:

self.dtOptions = DTOptionsBuilder.newOptions().withButtons(["print"]);

I have datatables inside div that is shown using ng-if, but this can't be an issue.
Thanks for advice!

@Misiu
Copy link
Author

Misiu commented Mar 7, 2016

For now I'm using this:

self.dtOptions = DTOptionsBuilder.newOptions().withButtons(["print"]).withLanguage({
    "processing": "Przetwarzanie...",
    "search": "Szukaj:",
    "lengthMenu": "Pokaż _MENU_ pozycji",
    "info": "Pozycje od _START_ do _END_ z _TOTAL_ łącznie",
    "buttons": {
        "print": "Drukuj"
    }
});

inside my controller, but I'd like to set those options once and have them globally.

@l-lin
Copy link
Owner

l-lin commented Mar 7, 2016

Can you try using the old keys:

{
    "sProcessing": "Przetwarzanie...",
    "sSearch": "Szukaj:",
    "sLengthMenu": "Pokaż _MENU_ pozycji",
    "sInfo": "Pozycje od _START_ do _END_ z _TOTAL_ łącznie",
    "oButtons": {
        "sPrint": "Drukuj"
    }
}

@Misiu
Copy link
Author

Misiu commented Mar 8, 2016

This is odd, using old keys helped, but only partially.
Standard text are translated, but I get default "Print" on print button instead of translated text.

Could You take a look why new keys don't work when setting localization using JSON object but they work when using sUrl.

@l-lin l-lin added this to the v0.5.4 milestone Mar 8, 2016
@l-lin l-lin added the bug label Mar 8, 2016
@l-lin l-lin closed this as completed Mar 8, 2016
@Misiu
Copy link
Author

Misiu commented Mar 9, 2016

@l-lin thank You for fixing this so quickly.
I've looked at Your commit and noticed that You are using fn.DataTables (capital D), when DataTables docs say to use lower d, example here: https://datatables.net/examples/advanced_init/defaults.html
Could You please take a look :)

@l-lin
Copy link
Owner

l-lin commented Mar 9, 2016

Mmh indeed. It's quite confusing. I read somewhere that from v1.10+, we need to use the capital D. So I assumed it wass the same when setting the default settings...

@Misiu
Copy link
Author

Misiu commented Mar 9, 2016

Maybe Allan (@DataTables) could confirm which option ot use. On his site I've seen that when creating datatable we should use capital D, but all settings are set with lowercase d.

@DataTables
Copy link

The $().dataTable() v $().DataTable() constructors effect the object that is returned (jQuery and DataTables API respectively). They have no effect on the options that are passed in.

Sometimes it can be useful to have a jQuery instance referring to the table (a lot of jQuery plug-ins to that - probably most of them in fact), and sometimes you want the DataTables specific API.

The legacy DataTables API modified the jQuery object and that was the wrong thing to do (legacy software... :-) ). The legacy API will be removed in the next major version (although a backwards compatibility file will be available).

@Misiu
Copy link
Author

Misiu commented Mar 9, 2016

Allan (@DataTables) thanks for clarification.
So summarizing, we should extend DataTable's default settings using capital D, like this:

$.extend(true, $.fn.DataTable.defaults, {
    language: json
});

Am I right?

@DataTables
Copy link

Not fully :-). You'll find that $.fn.dataTable.defaults === $.fn.DataTable.defaults.

As I say, the only difference is in the return from the constructor. Otherwise the two objects should be identical.

@Misiu
Copy link
Author

Misiu commented Mar 9, 2016

@l-lin I've tried both versions:

function setLanguage(language) {
    $.extend(true, $.fn.dataTable.defaults, {
        language: language
    });
    return options;
}

and

function setLanguage(language) {
    $.extend(true, $.fn.DataTable.defaults, {
        language: language
    });
    return options;
}

and they both work, so I was able to set language using DTDefaultOptions.setLanguage
Your commit is fine 😄

@amjarosik
Copy link

DTDefaultOptions.setLanguage({
'sProcessing': 'Przetwarzanie...',
'sSearch': 'Szukaj',
'oPaginate': {
'sNext': 'Następny',
'sPrevious': 'Poprzedni',
'sLast': 'Ostatni'
},
'sLengthMenu': 'Pokaż MENU pozycji',
'sInfo': 'Pozycje od START do END z TOTAL łącznie'

});

@amjarosik
Copy link

amjarosik commented Apr 26, 2016

globally old way without necessary requesting like
DTDefaultOptions.setLanguage({
sUrl: "../Content/pl.js"
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants