datePipe Angular中的i18n [英] i18n in datePipe Angular

查看:130
本文介绍了datePipe Angular中的i18n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an application that it's running in two language ( i can change an choose the language i want bu using i18n) English / French.

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

At the moment i can get the date only in english even if i select the French Language.

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

此刻我想在2018年8月17日星期一得到它,我也想在法国买到隆迪17年Aout 2018

At the moment i get it like this Monday 17 August 2018, i want to get the french one too Lundi 17 Aout 2018

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

Is there a way to change the date depending on what language is selected ?

推荐答案

创建并使用自定义管道,如下所述: https://angular.io/guide/管道

Create and use a custom pipe as described here: https://angular.io/guide/pipes

类似这样的东西:

import { Pipe, PipeTransform } from '@angular/core';

enum Days {
    Dimanche,
    Lundi,
    Mardi,
    Mercredi,
    Jeudi,
    Vendredi,
    Samedi
}

enum Months {
    Janvier,
    Février,
    Mars,
    Avril,
    Mai,
    Juin,
    Juillet,
    Août,
    Septembre,
    Octobre,
    Novembre,
    Décembre
}

@Pipe({ name: 'frenchDate' })
export class FrenchDatePipe implements PipeTransform {
    transform(date: Date) {

        const dayOfMonth = date.getDate();
        const nameOfDay = Days[date.getDay()];
        const nameOfMonth = Months[date.getMonth()];
        const year = date.getYear();

        const result = nameOfDay + ' ' + dayOfMonth + ' ' + nameOfMonth + ' ' + year;

       return result;
    }
}

这篇关于datePipe Angular中的i18n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆