javascript - 使用 Material Angular 10 的格式更改 Datepicker 的语言

标签 javascript angular typescript angular-material angular-i18n

我的应用程序中确实有多语言支持,并且希望实现 Angular Material 日期选择器的翻译。我使用了 Material 中的 dateAdapter 类并设置了值,但是在这样做的同时,我的显示格式正在发生变化。
有人遇到过同样的问题吗?

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');
}

最佳答案

对于多语言支持,我建议您使用 MomentDateAdapter .这是 Angular 文档中关于多语言支持和 NativeDateAdapter(默认的)的注释:

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 shortcomings of the native Date object is the inability to set the parse format. We highly recommend using the MomentDateAdapter or a custom DateAdapter that works with the formatting/parsing library of your choice.


唯一的对应物是使用 MomentDateAdapter您现在将拥有 moment作为依赖...但没什么大不了的,您可能已经在使用它。
这是一些示例代码(取自 Angular 文档):
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 component 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 component 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');
  }
}

关于javascript - 使用 Material Angular 10 的格式更改 Datepicker 的语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64793828/

相关文章:

typescript - 使用 Electron 时,我应该如何为 Angular 2 配置基本 href?

reactjs - 在 React 项目上运行 "Invalid option for project: true"时会产生什么 'tslint --project'?

visual-studio-2013 - 如何在 Visual Studio 中将 TypeScript 输出合并到多个文件中

javascript - 重新映射 json 对象以进行消息传递

javascript - 如何从 document.form 获取 html 元素

javascript - 在javascript中查找特定单词(正则表达式)中的字符类型

javascript - 在 Reactjs 中退出全屏

javascript - Angular 5 如何检测我们正在生产中的index.html

angular - 在 Angular 2 中,如何将复选框值发送到组件中的函数?

html - 将第二个全长行添加到垫表中的每个垫行