如何对日期DatePipe使用ngx-translate?

时间:2018-07-19 11:09:04

标签: angular ionic-framework ngx-translate

我有一个应用程序,它以两种语言(英语,法语)运行(我可以使用i18n更改我想选择的语言)。

此刻,即使选择法语,我也只能用英语获取日期。

   <div class="information">
              {{ information.date | information:'EEEE'}} {{information.date | date:'d'}} {{ information.date | date:'MMMM'}} {{ information.date |
              date:'yyyy'}}
        </div>

有没有一种方法可以根据我选择的语言来更改日期?

1 个答案:

答案 0 :(得分:0)

您可以将类变量用作日期格式,例如{{information.date | date:dateFormat}},并在语言更改时更改日期格式

@Component(...)
export class WhateverComponent {

    dateFormat: string;

    dateFormatSubscription: Subscription;

    constructor(translateService: TranslateService) {
        const langToDateFormat = lang => {/* map language to date format HERE */}
        this.dateFormat = langToDateFormat(translateService.currentLang);
        this.dateFormatSubscription = translateService.onLangChange().pipe(
            map(langToDateFormat)
        ).subscribe(dateFormat => this.dateFormat = dateFormat);
    }

    ngOnDestroy() {
        // don't forget to unsubscribe!!!
        this.dateFormatSubscription.unsubscribe();
    }
}
相关问题