i18n in datePipe Angular

一世执手 提交于 2019-12-23 04:19:10

问题


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>

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 ?


回答1:


Create and use a custom pipe as described here: 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;
    }
}



回答2:


I have no more knowledge about i18n but after study docs i ensure that this code will work well for you .

ng serve --configuration=fr

use this command for run your program .here fr refers to the French language identifier.

If you want to import locale data for other languages, you can do it manually in app.module.ts file.

import { registerLocaleData } from '@angular/common';

import localeFr from '@angular/common/locales/fr';

registerLocaleData(localeFr, 'fr');



来源:https://stackoverflow.com/questions/51435941/i18n-in-datepipe-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!