angular / components

Component infrastructure and Material Design components for Angular
https://material.angular.io
MIT License
24.38k stars 6.75k forks source link

bug(Datepicker): Datepicker with Moment ignores locale #28263

Open mzeitz1902 opened 11 months ago

mzeitz1902 commented 11 months ago

Is this a regression?

The previous version in which this bug was not present was

16

Description

Since updating to Angular 17, my mat-datepicker ignores my locale. Everything should be in German but instead it's all in English now.

Reproduction

StackBlitz link: https://components-issue-uudyxr.stackblitz.io Steps to reproduce:

  1. Open the datepicker to select a date
  2. Select a year and a month

Expected Behavior

The months are displayed in German (screenshot taken from production environment that doesn't have the update to Angular 17 yet).

image image

Actual Behavior

The months are displayed in English

image image

Environment

jahusa02 commented 11 months ago

We have the same problem.

Do you use the new ESbuild builder?

mzeitz1902 commented 11 months ago

Yes, we do

jahusa02 commented 11 months ago

Yes, we do

I guess it's a Problem with that. Can't find the Issue Link rn but will provide it later. Since the ESBuild Builder, the default locale is always 'en_US'

mzeitz1902 commented 11 months ago

Okay, but can we fix that somehow or is it in fact a bug?

jahusa02 commented 11 months ago

Didn't find a way yet. We reverted back to Webpack for the moment

Sebi11 commented 11 months ago

Same issue here, with my newly migrated app to Angular 17, using the new Vite builder.

I cannot find the other issue you're talking about @jahusa02 but I'd be glad to read it as well in case I could provide a workaround!

EDIT: I ran a few tests. It seems the bug comes from Moment library in fact: the method moment.localeData(str) always return the same object (English locale). I tried moment.localeData('fr') directly in my main app which leads to the bug, and then the bug occurs in the Materiel Datepicker as well.

mzeitz1902 commented 11 months ago

So, do you guys think this bug will be adressed soon? I really don't want to go back to Angular 16 or the old builder.

jahusa02 commented 10 months ago

Same issue here, with my newly migrated app to Angular 17, using the new Vite builder.

I cannot find the other issue you're talking about @jahusa02 but I'd be glad to read it as well in case I could provide a workaround!

EDIT: I ran a few tests. It seems the bug comes from Moment library in fact: the methodmoment.localeData(str)always return the same object (English locale). I triedmoment.localeData('fr')` directly in my main app which leads to the bug, and then the bug occurs in the Materiel Datepicker as well.

I can't find it anymore 😅 sorry. Closest Issue I found is this one: https://github.com/angular/angular-cli/issues/26409#issuecomment-1825420474

mzeitz1902 commented 10 months ago

Hmm I tried removing import '@angular/localize/init';. Didn't fix it though

CodingMonkeyN commented 10 months ago

You can use a workaround like: import 'moment/locale/de';

This way the locales get compiled and work again.

Michriko commented 10 months ago

Had the same issue. Also used the workaround @CodingMonkeyN suggested.

mzeitz1902 commented 10 months ago

Thank you @CodingMonkeyN ! This works :)

BenjaminBrodwolf commented 5 months ago

Have the same issue but I don't understand this workaround. Where I have to add this import 'moment/locale/de'; ?

CodingMonkeyN commented 5 months ago

You need to adjust your imports where you normally import Moment with the import specifying the wanted locales.

raudzi commented 4 months ago

I'm still having problems with this. We also use the new application builder. For ng serve it works as intended however for our prod build moment locales throw an error "Cannot read properties of undefined (reading 'defineLocale')". image

This is how we import moment together with the locales: image

What I see in Chrome: image

So apparently everything is shipped correctly which makes sense since it's the de.js that's throwing the error. We initially had some issues with the esbuild because of the "baseHref" option. I understand way to little about bundling etc. to figure out if this could be related. I'd be greatful for any ideas! Let me know if you need additional info of me.

raudzi commented 4 months ago

Okay nevermind my issue has something to do with scoping. Someone actually defined a global variable called "require" facepalm, hence, this check failed inside the locales image

The fix of @CodingMonkeyN works for me now as well :D

mcvendrell commented 3 months ago

You can use a workaround like: import 'moment/locale/de';

This way the locales get compiled and work again.

OMG, 2 hours fighting with multiple locales configurations and it was simply a bug... Your solution worked for me, even setting it in bootstrap app.config.ts Thank you.

carlosdag28 commented 2 months ago

Hi there, issue still happening on Angular Material v18 + Moment v2.30.1

Commented workaround is working for me too