Internationalization of datepickers
Asked Answered
A

2

6

I try to force datepicker in Angular 5 project generated by jHipster to use polish locale, for days of week and months. As I understand regarding to documentation:

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

it should work with default settings, which are:

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

usage

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

Unfortunately it doesn't work, I also tried different approaches, like:

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

or

https://github.com/NativeScript/nativescript-angular/issues/1147 - where I copied the locale file

Have you got any ideas what might be wrong?

UPDATE

I finally solved it like that:

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

shared-common.module.ts

{ provide: NgbDatepickerI18n, useClass: CustomDatepickerI18n }

Will do the job for me, but any improvement suggestions appreciated.

Avivah answered 17/6, 2018 at 20:29 Comment(0)
D
0

i don't use datepicker but in the ngx-translate, i have the same issue.
in app.module.ts, i add:
import localEn from '@angular/common/locales/en'; registerLocaleData(localEn);

Duodenum answered 18/6, 2018 at 2:5 Comment(1)
I tried that as well, to register locale in app.module, and that doesn't solve the issue, but thanks.Avivah
T
0

Besides registering registering the local you should also set it

This solved the problem for me https://angular.io/api/core/LOCALE_ID

providers: [
 {provide: LOCALE_ID, useValue: 'he-IL' }
]

in the app.module.ts file.

Tuddor answered 9/6, 2020 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.