国际化的datepickers

时间:2018-06-17 20:29:45

标签: angular datepicker localization angular5 ng-bootstrap

我尝试强制jhipster生成的Angular 5项目中的datepicker使用波兰语语言环境,为数周和数月。据我所知,文档:

https://ng-bootstrap.github.io/#/components/datepicker/examples#i18n

它应该使用默认设置,即:

共享common.module.ts

...
import { registerLocaleData } from '@angular/common';
import locale from '@angular/common/locales/pl';
...

providers: [
    ...
    {
        provide: LOCALE_ID,
        useValue: 'pl'
    },
],
...
export class TestSharedCommonModule {
    constructor() {
        registerLocaleData(locale);
    }
}

使用

<input id="field_testDate" type="text" class="form-control" name="testDate" 
ngbDatepicker  #testDateDp="ngbDatepicker" [(ngModel)]="tester.testDate"
            required/>

不幸的是它不起作用,我也尝试了不同的方法,例如:

https://angular.io/api/core/LOCALE_ID

https://github.com/NativeScript/nativescript-angular/issues/1147 - 我复制了语言环境文件

你有什么想法可能有什么问题吗?

更新

我终于解决了这个问题:

日期选择器-i18n.ts

import { Inject, Injectable, LOCALE_ID } from '@angular/core';
import { NgbDatepickerI18n } from '@ng-bootstrap/ng-bootstrap';

const I18N_VALUES = {
    en: {
        weekdays: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
        months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    },
    pl: {
        weekdays: ['Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'So', 'N'],
        months: ['Sty', 'Lut', 'Mar', 'Kwi', 'Maj', 'Cze', 'Lip', 'Sie', 'Wrz', 'Paź', 'Lis', 'Gru'],
    }
};

@Injectable()
export class CustomDatepickerI18n extends NgbDatepickerI18n {

    constructor(@Inject(LOCALE_ID) private locale: string) {
        super();
    }

    getWeekdayShortName(weekday: number): string {
        return I18N_VALUES[this.locale].weekdays[weekday - 1];
    }
    getMonthShortName(month: number): string {
        return I18N_VALUES[this.locale].months[month - 1];
    }
    getMonthFullName(month: number): string {
        return this.getMonthShortName(month);
    }
}

共享common.module.ts

{ provide: NgbDatepickerI18n, useClass: CustomDatepickerI18n }

将为我做这份工作,但任何改进建议都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

我不使用datepicker但是在ngx-translate中,我有同样的问题。
app.module.ts中,我添加:
import localEn from '@angular/common/locales/en'; registerLocaleData(localEn);

答案 1 :(得分:0)

除了注册本地外,还应该设置它

这为我https://angular.io/api/core/LOCALE_ID

解决了问题
providers: [
 {provide: LOCALE_ID, useValue: 'he-IL' }
]

在app.module.ts文件中。