简体   繁体   English

datePipe Angular 中的 i18n

[英]i18n in datePipe Angular

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.我有一个应用程序,它以两种语言运行(我可以使用 i18n 更改选择我想要的语言)英语/法语。

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>

At the moment i get it like this Monday 17 August 2018, i want to get the french one too Lundi 17 Aout 2018目前我在 2018 年 8 月 17 日星期一得到它,我也想得到法国的 Lundi 17 Aout 2018

Is there a way to change the date depending on what language is selected ?有没有办法根据选择的语言更改日期?

Create and use a custom pipe as described here: https://angular.io/guide/pipes按照此处所述创建和使用自定义管道: https : //angular.io/guide/pipes

Something like this:像这样的东西:

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

I have no more knowledge about i18n but after study docs i ensure that this code will work well for you .我对 i18n 没有更多的了解,但在研究文档后,我确保此代码适合您。

ng serve --configuration=fr ng serve --configuration=fr

use this command for run your program .here fr refers to the French language identifier.使用此命令运行您的程序。这里fr指的是法语标识符。

If you want to import locale data for other languages, you can do it manually in app.module.ts file.如果要导入其他语言的区域设置数据,可以在app.module.ts文件中手动执行

import { registerLocaleData } from '@angular/common';从 '@angular/common' 导入 { registerLocaleData };

import localeFr from '@angular/common/locales/fr';从'@angular/common/locales/fr'导入localeFr;

registerLocaleData(localeFr, 'fr'); registerLocaleData(localeFr, 'fr');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM