danielmoncada / date-time-picker

Angular Date Time Picker (Responsive Design)
https://daniel-projects.firebaseapp.com/owlng/date-time-picker
MIT License
140 stars 60 forks source link

Changing the locale of the datepicker throws error #110

Closed SudarshanPH closed 3 years ago

SudarshanPH commented 3 years ago

Hey, if we import { DateTimeAdapter, OwlDateTimeComponent } from 'ng-pick-datetime'; and set locale using this.dateTimeAdapter.setLocale(' fr '), datepicker throws error without displaying anything.

Screenshot of the Initial date-time picker Screenshot from 2021-02-08 11-02-24

Screenshot of the Date-time picker after changing the locale using this.dateTimeAdapter.setLocale(' fr '), (Note that we get error for all languages) image_2021_02_08T05_33_26_508Z.

Can we get this issue resolved. Is there any alternate way to work with dynamic change in end user language?

sudarshan-2012 commented 3 years ago

any update on this @danielmoncada?

fdc80 commented 3 years ago

Same problem with de, fr, it, es... any ideas?

fdc80 commented 3 years ago

I partially solved with import

import localeIt from '@angular/common/locales/it'; import localeDe from '@angular/common/locales/de'; import localeEs from '@angular/common/locales/es'; import localeFr from '@angular/common/locales/fr'; import { DateTimeAdapter, OwlDateTimeIntl } from '@danielmoncada/angular-datetime-picker';

and

registerLocaleData(localeIt, 'it'); registerLocaleData(localeDe, 'de'); registerLocaleData(localeEs, 'es'); registerLocaleData(localeFr, 'fr'); dateTimeAdapter.setLocale(my_internal_language_variable);

in my class constructor, but still remains "set" and "cancel" missing translation. Months and week days are ok.

SudarshanPH commented 3 years ago

yes @fdc80 , that is a good workaround but if we want it to work dynamically to many other languages, we cannot always import all the locales right

davewicklund commented 3 years ago

Thank you very much @fdc80 your pointers were perfect!

What I did was dynamically load the locale I needed. I followed Michaels post: https://indepth.dev/posts/1038/dynamic-import-of-locales-in-angular

and added this method: importAndLoadLocale(locale) {

import('@angular/common/locales/' + locale + '.js').then(module => {
  registerLocaleData(module.default);
  this.dateTimeAdapter.setLocale(locale);
});

}

My War file is huge with an extra 3200 locale files, but only the one I need is lazily loaded at run time.

Michael also points out that you can limit the languages, but for now, I am going with the 3200 files.

My last issue is the Set and Cancel buttons. I would like to dynamically translate them after I load my language file. I am using ngx-translate.

Any ideas for that?

Thanks, Dave

SudarshanPH commented 3 years ago

Hi @davewicklund , in our case, we send hours and minutes static string to a language api which takes care of translations

danielmoncada commented 3 years ago

Sorry, I've been at bit MIA due to the craziness at work lately. I'll do my best to look into this issue.

JoostK commented 3 years ago

We got bitten by this same issue; I believe it is due to https://github.com/danielmoncada/date-time-picker/pull/10/commits/407a7f8623f2ee42251e94415f9a2d382ea99b9a which starts to use getLocaleFirstDayOfWeek, thereby adding a dependency on the registered Angular locales. We used to derive the first day of the month using moment's locale data, but now the default first day of the week is computed eagerly which crashes due to missing Angular locale data.

I opened #115 that addresses this issue.

danielmoncada commented 3 years ago

fixed in latest version 11.0.1, thanks @JoostK !