momentLocale is being ignored - Editor 2.0.6

momentLocale is being ignored - Editor 2.0.6

rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

I am still trying to migrate from Editor 1.9.3 to Editor 2.0.6.

I am using moment.js for my bilingual website (momentLocale: "de" or "en-gb" as opposed to the American "en").

This is the code I use:

if (lang === 'de') {
    moment.locale('de');
    momentLocale = 'de';
} else {
    moment.locale('en-gb');
    momentLocale = 'en-gb';
}

 // https://datatables.net/blog/2014-12-18  Ultimate date/time sorting
$.fn.dataTable.moment( 'L', momentLocale );

//Editor datetime field definition:
.....
}, {
    label: lang === 'de' ? 'Ende (bzw. Zinsbindung):' : 'End Date:',
    name: "fixed.end_date",
    attr: {
        class: dateMask
    },
    type: "datetime",
    def: function () {
        return '';
    },
    format: 'L',
    // format: "DD.MM.YYYY",
    opts: {
        showWeekNumber: true,
        yearRange: 40,
        momentLocale: momentLocale
    }  
}, {
.....

These are the moment files I use:

<!-- Moment.js: -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<!-- Locales for moment.js-->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/de.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/en-gb.js"></script>

If you take a look at the locale files you can see what format "L" means:
- en-gb: L : 'DD/MM/YYYY',
- de: L : 'DD.MM.YYYY',

If I hard code those formats like in the commented line above (format: "DD.MM.YYYY",): Works. If I don't (format: 'L',), the American format MM/DD/YYYY is returned from the date picker. Hence I believe the moment settings are partly being ignored by the new Datetime library for Editor. All of this had worked perfectly in Editor 1.9.3 and previous versions.

This is what it looks like using "L" as the format:
The date picker looks normal with language "de". But now it ignores the date that had already been set, opens the current month and preselects the current day which is another error.

Picking the date 14 February 2022 you get this (American format with German date points inside due to date masking that I use)

This German looking date isn't only wrong but it is also invalid because there is no month 14.

This question has accepted answers - jump to:

Answers

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

    Just did a quick compare of the Editor 1.9.3 code and the code of the new DateTime-1.1.2 lib.

    While the former has 5 occurences of "momentLocale" the latter has ZERO. Looks like that "momentLocale" is being ignored ... Is that intentionally so?

    It is still in the dateTime docs:
    https://editor.datatables.net/reference/field/datetime

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

    Found "locale" in the new library. 3 times. So the locale wasn't discarded, just renamed. :smile:

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

    And the last one for today: There was nothing in the console and no obvious error on the client side - only PHP crashed due to the unexpected American date format sent to the server.

  • allanallan Posts: 61,669Questions: 1Answers: 10,096 Site admin

    Sorry - DateTime supports Luxon as well as Moment, which is why it removed mention of the momentLocale option. However, Editor hasn't actually been updated to match that, so at the moment you'll need to specify both local and momentLocale:

    momentLocale: '...',
    opts: {
      locale: '...'
    }
    

    That's obviously redundant and I'll get that fixed.

    Allan

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

    ok, in the meantime I implemented a hack by just renaming "locale" with "momentLocale" in the combined Editor / Datetime code.

    I very much hope that I won't have to rename the 196 occurences of "momentLocale" in my code with "locale"!

    How will I learn that you made the change? Thanks.

  • allanallan Posts: 61,669Questions: 1Answers: 10,096 Site admin
    Answer ✓

    Oh wow - that's a lot setting of that value! We perhaps need to have a way to define defaults for this values.

    Currently I've got it scheduled for Editor 2.1 to address this, that is probably a few months away. As a quick fix for your use case search for this in the Editor JS:

            conf._picker = new DataTable.DateTime(conf._input, $.extend({
                format: conf.displayFormat || conf.format, // can be undefined
                i18n: this.i18n.datetime,
            }, conf.opts));
    

    Change it to be:

            conf._picker = new DataTable.DateTime(conf._input, $.extend({
                format: conf.displayFormat || conf.format, // can be undefined
                i18n: this.i18n.datetime,
                locale: conf.momentLocale
            }, conf.opts));
    

    Allan

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

    Oh wow - that's a lot setting of that value! We perhaps need to have a way to define defaults for this values.

    Well, it has become a pretty large application with three subsystems in the meantime. If I could define defaults for this stuff like below that would be highly appreciated! Thanks for the update!

    $.extend( true, $.fn.dataTable.Editor.defaults, {
        formOptions: {
            main: {
                onBackground: 'none',
                onReturn: 'none'
            },
            bubble: {
                onBackground: 'none'
            }
        }
    });
    
  • allanallan Posts: 61,669Questions: 1Answers: 10,096 Site admin
    Answer ✓

    Yes, I think that is basically the sort of structure that would be needed.

    I'll look into that for 2.1 as well.

    Allan

  • allanallan Posts: 61,669Questions: 1Answers: 10,096 Site admin
    Answer ✓

    An old thread this - sorry for the massive delay! I've sorted this out for 2.1 now, which should drop before the end of the month (that's the plan anyway!). momentLocale and momentStrict are now propagated into the DateTime instance (locale and strict). Furthermore, the Editor part of the code doesn't actually use Moment / Luxon at all now - instead relying on DateTime to do all transforms required, so the locale will be consistent for all operations on the field.

    momentLocale and momentStrict are legacy parameters and are now removed from the documentation. They will continue to work as expected due to the mapping, but new projects should use the opts.locale and opts.strict options for DateTime.

    Allan

Sign In or Register to comment.