1

This example is in Angular Material documentation for dynamically changing locale for date picker component. But it doesn't work. What's weird is that the code snippet on documentation page works but the stackblitz doesn't.

3 Answers 3

3

If you want to change the language of your component you need to declare in your Constructor the DateAdapter, and you set the language like this:

constructor(private dateAdapter: DateAdapter<Date>) {
    this.dateAdapter.setLocale('it');
}

In my example i set the language in Italian.

Sign up to request clarification or add additional context in comments.

1 Comment

I do all of the internationalization related things in my app with a service.
1

There seem to be some issues in angular DateAdapter according local settings.

E.g.:

As workaround the logic moved into a lazy loaded module seems to work

Demo Stackblitz

  1. Clicking the "test" link loads the lazy /test route. Both, app.module and lazy.module import SharedModule which imports the Material stuff.
  2. The datepicker is in English by default. Clicking the button "set locale on app module" should set it to German, but it doesn't.
  3. Clicking the button "set locale in lazy module" however works as expected.

Workaround sources:

https://github.com/angular/components/issues/12891#issue-355191637

https://github.com/angular/components/issues/15419#issuecomment-491772822

Comments

0

It is possible to change locale dynamically for Angular material date picker.

working example - https://codesandbox.io/s/material-date-locale-jyylz

2 Comments

It doesn't work.
Sir @M.AlJumaily plz check carefully. steps - open Datepicker-> check the month name (it is in French) -> click on the button 'Switch to Current Locale' (my current locale is English/en) -> open the Datepicker again -> now month name is in English format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.