Search code examples
angularmomentjsangular11date-pipeangular-date-format

how to translate angular datePipe's dates with moment.js?


I have this version of packages:

"@angular/cdk": "^11.2.13",
"@ngx-translate/core": "^13.0.0",
"@angular/material-moment-adapter": "^12.2.9",
"moment": "^2.29.1",
"@types/moment": "^2.13.0",

I am using angular datePipe | date: 'MMM d, y' format, which is mediumDate. I want to translate it to other language. For example Instead of Jun 15, 2015 , I want to be 'ივნ 15,2015".

shared.module.ts

export function culture() {
    const locale = localStorage.getItem('culture');
    return locale ? locale.substr(0, locale.indexOf('-')) : 'ka';
}
 providers: [
        { provide: MAT_DATE_LOCALE, useValue: culture() },
        { provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },
        { provide: MAT_DATE_FORMATS, useValue: MAT_MOMENT_DATE_FORMATS }
    ]

app.module.ts

moment.locale(culture());

Also I use angular material datepicker and it translates.


Solution

  • I added in the shared module

    import ka from '@angular/common/locales/ka';
    registerLocaleData(ka);
    providers: [
       DatePipe,
    ]
    

    and Add alseo localized date pipe which transform dates according to currentLanguage.

    there is solution linksolution