admin管理员组文章数量:1405411
I do have Multilanguage support in my application and would like to implement translation for the angular material date picker. I have used dateAdapter class from material and set the values but while doing so my format of display is getting changes.
Is anyone have faced same issue?
export const MY_FORMATS = {
parse: {
dateInput: 'LL',
},
display: {
dateInput: 'ddd, MMM. D YYYY',
monthYearLabel: 'MMM YYYY',
dateA11yLabel: 'LL',
monthYearA11yLabel: 'MMMM YYYY',
},
};
@Component({
selector: 'test',
templateUrl: './test.html',
styleUrls: ['./test.scss'],
providers: [{ provide: MAT_DATE_FORMATS, useValue: MY_FORMATS }],
})
ngOnInit(): void {
//on language change
//change language
this.dateAdapter.setLocale('fr');
}
I do have Multilanguage support in my application and would like to implement translation for the angular material date picker. I have used dateAdapter class from material and set the values but while doing so my format of display is getting changes.
Is anyone have faced same issue?
export const MY_FORMATS = {
parse: {
dateInput: 'LL',
},
display: {
dateInput: 'ddd, MMM. D YYYY',
monthYearLabel: 'MMM YYYY',
dateA11yLabel: 'LL',
monthYearA11yLabel: 'MMMM YYYY',
},
};
@Component({
selector: 'test',
templateUrl: './test.html',
styleUrls: ['./test.scss'],
providers: [{ provide: MAT_DATE_FORMATS, useValue: MY_FORMATS }],
})
ngOnInit(): void {
//on language change
//change language
this.dateAdapter.setLocale('fr');
}
Share
Improve this question
edited Nov 13, 2020 at 20:55
app
asked Nov 11, 2020 at 20:41
appapp
20711 silver badges31 bronze badges
1 Answer
Reset to default 6 +25For multilanguage support I would remend you to use the MomentDateAdapter. Here is one note from Angular docs about multi-language support and the NativeDateAdapter (the default one's):
MatNativeDateModule is based off the functionality available in JavaScript's native Date object. Thus it is not suitable for many locales. One of the biggest shortings of the native Date object is the inability to set the parse format. We highly remend using the MomentDateAdapter or a custom DateAdapter that works with the formatting/parsing library of your choice.
The only counterpart is that by using MomentDateAdapter
you will now have moment
as a dependency... but not big deal, and you may already be using it.
Here is some example code (taken from Angular docs):
import {Component} from '@angular/core';
import {
MAT_MOMENT_DATE_FORMATS,
MomentDateAdapter,
MAT_MOMENT_DATE_ADAPTER_OPTIONS,
} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
/** @title Datepicker with different locale */
@Component({
selector: 'test',
templateUrl: 'test.html',
styleUrls: ['test.css'],
providers: [
// The locale would typically be provided on the root module of your application. We do it at
// the ponent level here, due to limitations of our example generation script.
{provide: MAT_DATE_LOCALE, useValue: 'fr'},
// `MomentDateAdapter` and `MAT_MOMENT_DATE_FORMATS` can be automatically provided by importing
// `MatMomentDateModule` in your applications root module. We provide it at the ponent level
// here, due to limitations of our example generation script.
{
provide: DateAdapter,
useClass: MomentDateAdapter,
deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
},
{provide: MAT_DATE_FORMATS, useValue: MAT_MOMENT_DATE_FORMATS},
],
})
export class DatepickerLocaleExample {
constructor(private _adapter: DateAdapter<any>) {}
// Change adapter language to japanese
japanese() {
this._adapter.setLocale('ja-JP');
}
}
本文标签: javascriptChange language of Datepicker with maintaining format of Material Angular 10Stack Overflow
版权声明:本文标题:javascript - Change language of Datepicker with maintaining format of Material Angular 10 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744228677a2596218.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论