2

I have quite strange issue. I'm using jQuery 3, Boostrap 3 and this DateTimepicker. The problem is that in datetime picker at first attempt I'm always getting invalid locale (always English), but on second attempt (and further) it's valid. The end of my website look like this:

 <script>
    $.when($.ready).then(function () {
        moment().locale('da');

        $('.datetimepicker').datetimepicker({
            locale: 'da',
            viewMode: 'years',
            format: 'YYYY-MM-DD',
            defaultDate: false,
            useCurrent: false,
            viewDate: '1999-04-20'
        });
    })
</script>
</body>
</html>

I've added this moment().locale('da'); because I thought it would solve the issue but it doesn't in my case.

Any clue how can I force this to make it work?

If it's important, I'm using WebPack and my JS file looks like this:

window.$ = window.jQuery = require('jquery');

require('bootstrap-sass');
require('bootstrap-select');
require('eonasdan-bootstrap-datetimepicker');

var moment = require('moment');
window.moment = moment;

It seems that when I change in this file:

var moment = require('moment');
window.moment = moment;

to

var moment = require('moment');
moment.locale('da');
window.moment = moment;

Locale is set in valid way at 1st attempt. Unfortunately I have multiple languages and probably it doesn't make much sense to have multiple JS files just to make moment use valid locale.

Is there any way to solve this?

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • Moment provides a `moment-with-locales` files that includes each supported locale, I don't know exactly how to use in it in webpack, but maybe [this section](http://momentjs.com/docs/#/use-it/require-js/) of the docs could help you. Once you loaded the locale files you need, you can dinamically change datetimepicker locale using its [`locale`](http://eonasdan.github.io/bootstrap-datetimepicker/Functions/#locale) function. – VincenzoC Apr 20 '17 at 18:35
  • Could you create a demo reproducing this issue on JSFiddle? – Michał Perłakowski Apr 20 '17 at 18:38
  • 2
    BTW why are you using `$.when($.ready).then(function () {})` instead of just `$(function() {})`? – Michał Perłakowski Apr 20 '17 at 18:41
  • @MichałPerłakowski Good point for notation, I have no idea, probably saw it somewhere in docs and thought this notation has to be used in jQuery 3 (previously I've been using v2) – Marcin Nabiałek Apr 20 '17 at 19:23

1 Answers1

0

It seems it was easy. I've used:

moment().locale('da');

what was causing no errors in JS console, but to make it work it has to be

moment.locale('da');
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • 1
    `moment().locale('da');` [_changes locales locally_](http://momentjs.com/docs/#/i18n/instance-locale/) (only for the given moment instance) while `moment.locale('da');` [_changes locales globally_](http://momentjs.com/docs/#/i18n/changing-locale/), that's why you have to use the latter. Anyway, after reading the last part of your question, I thought the you already tried that with no luck. – VincenzoC Apr 20 '17 at 20:16
  • @VincenzoC Thanks for explanation. No, I haven't tried it before (don't know why). I'm not front-end expert at all:) – Marcin Nabiałek Apr 20 '17 at 20:35