Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

How to set locale #33

Open
breitembach opened this issue Nov 11, 2018 · 3 comments
Open

How to set locale #33

breitembach opened this issue Nov 11, 2018 · 3 comments

Comments

@breitembach
Copy link

how i can set another locale please?

@jrauschenbusch
Copy link

As it seems the locale is actually only interpreted by the original mat-datepicker but, but not by the timepicker extension.

@matheo
Copy link
Contributor

matheo commented May 22, 2019

@breitembach @jrauschenbusch you need to provide a Custom MatDatepickerIntl like:

import { MatDatepickerIntl } from '@coachcare/datepicker';

@NgModule({
  ...
  providers: [{ provide: MatDatepickerIntl, useClass: MyDatepickerIntl }]
})

and it works in the same way as the official Intl class, but following this interface:
https://github.com/selvera/npm-datepicker/blob/master/datepicker/src/lib/datepicker-intl.ts

@jrauschenbusch
Copy link

jrauschenbusch commented May 23, 2019

@matheo If you only want to localize the labels and messages of the datepicker, then you're right. See
https://material.angular.io/components/datepicker/overview#localizing-labels-and-messages for more details.

But if you want to localize the date format it should be sufficient to import the MatMomentDateModule and to set the locale as desired. One of the following approaches should then be sufficient to change to date format of an matInput element value:

Either on module level:
{ provide: MAT_DATE_LOCALE, useValue: 'de-DE' }

or dynamically on component level:

constructor(private dateAdapter: DateAdapter<Date>) {
    dateAdapter.setLocale('de-DE')
}

This is currently not the case. Instead the following function is called 2 times. One time with a _datepicker.type date and one time with a _datepicker.type datetime. I think the first call is not intended, but maybe a side effect caused by the original matDatepicker code of @angular/material.

MatDatepickerInput.prototype._formatValue = function (value) {
        var type = this._datepicker.type;
        this._elementRef.nativeElement.value = value
            ? this._dateAdapter.format(value, this._dateFormats.display[type])
            : '';
 };

Additionally the value of this._dateFormats.display[type] should be 'lll' for type datetime instead of 'll h:mma' to provide a proper localization.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants