如何在不继承 NgbDatepickerI18n 的情况下为 Angular 中的 NgbDatepicker 进行翻译?

How to make translations for NgbDatepicker in Angular without subclassing NgbDatepickerI18n?

我目前正在阅读有关 ng-bootstrap 组件国际化的内容。他们的文档对日期选择器说:

Since the 2.0.0 release datepicker will use the application locale if it is present to get translations of weekdays and month names. The internal service that does translation is called NgbDatepickerI18n and you could provide your own implementation if necessary.

(ng-bootstrap website)

查看 Angular i18n 文档,它指出:

If you want to import locale data for other languages, you can do it manually:

src/app/app.module.ts

import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';

// the second parameter 'fr' is optional
registerLocaleData(localeFr, 'fr');

(Angular website)

但为什么它对我不起作用?我是否还需要自定义实现 NgbDatepickerI18n,还是我遗漏了什么?


这是一个游乐场示例:

https://stackblitz.com/edit/angular-mezpyn?file=app%2Fdatepicker-popup.module.ts

我遇到了同样的问题,因为没有答案,这是解决方案:

像您一样导入并注册语言环境数据后,在app.module.ts中您需要添加

providers: [
    { provide: LOCALE_ID, useValue: 'fr-FR' },
    ... other providers
],

我认为文档没有说清楚。