Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

localization - format number and date #409

Closed
xiejiabao opened this issue Feb 2, 2017 · 6 comments
Closed

localization - format number and date #409

xiejiabao opened this issue Feb 2, 2017 · 6 comments

Comments

@xiejiabao
Copy link

I cannot find the feature to do localization for number and date. Do I miss something? If not, will that be implemented in the future? I thing it is easy to do that. I suggest to use the ECMAScript Internationalization API - Init. Check Init here: https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Intl

thanks,

@emoralesb05
Copy link

emoralesb05 commented Feb 7, 2017

Why dont you use the angular pipes date and number? They follow locale of the language set as LOCALE_ID following the Intl.js lib

providers: [{
  provide: LOCALE_ID, useValue: 'fr',
}]

@xiejiabao
Copy link
Author

xiejiabao commented Feb 8, 2017

@emoralesb05 but the date pipe only can be used in chrome and opear
image
of course it is due to the Init API is limited to browser so far.

And we may would like to do the translation using the same way but no using different pipe.

Is it possible to consider develop localization for date and number in ng2-translate?

@ocombe
Copy link
Collaborator

ocombe commented Feb 8, 2017

You can use the intl polyfill to make it work everywhere...
Yes date & numbers are probably going to be in this lib one day, but I don't know when. If you want to make a PR for it, it'd be nice!

@rdukeshier
Copy link

FWIW - the following worked for me for dates. I assume the number related pipes would be equally trivial:

import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';
import { TranslateService } from '@ngx-translate/core';

@Pipe({
  name: 'localizedDate',
  pure: false  // required to update the value when currentLang is changed
})
export class LocalizedDatePipe implements PipeTransform {
  private value: string|null;
  private lastDate: any;
  private lastLang: string;

  constructor(private translate: TranslateService) { }

  transform(date: any, pattern: string = 'mediumDate'): any {
    const currentLang = this.translate.currentLang;

    // if we ask another time for the same date & locale, return the last value
    if (date === this.lastDate && currentLang === this.lastLang) {
      return this.value;
    }

    this.value = new DatePipe(currentLang).transform(date, pattern);
    this.lastDate = date;
    this.lastLang = currentLang;

    return this.value;
  }
}

@sonukapoor
Copy link

@rdukeshier keep in mind that setting the pipe pure property to false can create performance issues depending on many date pipes you are using. The pipe will get re-evaluated whenever a change-detection occurs.

@ocombe
Copy link
Collaborator

ocombe commented May 8, 2018

Hello, I'm closing this issue because it's too old.
If you have a similar problem with recent version of the library, please open a new issue.

The new i18n pipes in Angular no longer use the intl API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants