date }}

问题描述 投票:0回答:1
For all other localization purposes, I am using

(not

  • ). It gives me access to the current language (the user can switch language on the fly), but I don't know how I can link
  • with the language used in the

. Is there a way to do so in a simple way?DatePipe

In my Angular 8 app, I can't manage to display date fields in a localized form. I'd like to come up with something like the following: in English : "February 18, 2020 12:15:50" in French : "18 ...

angular provides a lot of locales, so use can register right locale which will be used in date pipe.ngx-translateThen you can specify locale in date pipeAngular i18nor overried default localeTranslateService#currentLangUPDATEDatePipeAngular i18n uses differential builds, but ngx-translate fetches needed locale on demand. As we can not combine those approaches, we can create custom date pipe, which would meet our requirements.

I created
angular date internationalization ngx-translate date-pipe
1个回答
3
投票

// app.module.ts

import { registerLocaleData } from '@angular/common';
import frenchLocale from '@angular/common/locales/fr';
import { LOCALE_ID, NgModule } from '@angular/core';

registerLocaleData(frenchLocale);

someDate | date: null: null: 'fr'

Yes, you can use

@NgModule({
  providers: [
    { provide: LOCALE_ID, useValue: "fr" }
  ]
})

DatePipe

. As you can see, it use

token be default. If you use Angular CLI, LOCALE_ID is provided automatically: A locale code for the locale format rules to use. When not supplied, uses the value of LOCALE_ID, which is en-US by default


0
投票

英文:"February 18, 2020 12: 15: 50"法语:"18 février 2020, 12:15:50"我想我应该使用Angular的 LOCALE_ID:

{{ myDate

© www.soinside.com 2019 - 2024. All rights reserved.