From 0a053a4cd52312e03179a3dfb5b6903aaa5a5a2e Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 26 Aug 2016 09:16:01 -0700 Subject: [PATCH] fix(i18n): Currency/Date/Number pipe use injected locale (#11093) --- .../@angular/common/src/pipes/date_pipe.ts | 9 ++++---- .../@angular/common/src/pipes/number_pipe.ts | 22 ++++++++++++------- .../common/test/pipes/date_pipe_spec.ts | 2 +- .../common/test/pipes/number_pipe_spec.ts | 6 ++--- tools/public_api_guard/common/index.d.ts | 4 ++++ 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/modules/@angular/common/src/pipes/date_pipe.ts b/modules/@angular/common/src/pipes/date_pipe.ts index 156ecc8563..6aa9195842 100644 --- a/modules/@angular/common/src/pipes/date_pipe.ts +++ b/modules/@angular/common/src/pipes/date_pipe.ts @@ -6,14 +6,14 @@ * found in the LICENSE file at https://angular.io/license */ -import {Pipe, PipeTransform} from '@angular/core'; +import {Inject, LOCALE_ID, Pipe, PipeTransform} from '@angular/core'; + import {StringMapWrapper} from '../facade/collection'; import {DateFormatter} from '../facade/intl'; import {DateWrapper, NumberWrapper, isBlank, isDate, isString} from '../facade/lang'; + import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception'; -// TODO: move to a global configurable location along with other i18n components. -var defaultLocale: string = 'en-US'; /** * Formats a date value to a string based on the requested format. @@ -96,6 +96,7 @@ export class DatePipe implements PipeTransform { 'shortTime': 'jm' }; + constructor(@Inject(LOCALE_ID) private _locale: string) {} transform(value: any, pattern: string = 'mediumDate'): string { if (isBlank(value)) return null; @@ -112,7 +113,7 @@ export class DatePipe implements PipeTransform { if (StringMapWrapper.contains(DatePipe._ALIASES, pattern)) { pattern = StringMapWrapper.get(DatePipe._ALIASES, pattern); } - return DateFormatter.format(value, defaultLocale, pattern); + return DateFormatter.format(value, this._locale, pattern); } private supports(obj: any): boolean { diff --git a/modules/@angular/common/src/pipes/number_pipe.ts b/modules/@angular/common/src/pipes/number_pipe.ts index d7ead4189b..ae6ebca4c2 100644 --- a/modules/@angular/common/src/pipes/number_pipe.ts +++ b/modules/@angular/common/src/pipes/number_pipe.ts @@ -6,19 +6,18 @@ * found in the LICENSE file at https://angular.io/license */ -import {Pipe, PipeTransform, Type} from '@angular/core'; +import {Inject, LOCALE_ID, Pipe, PipeTransform, Type} from '@angular/core'; import {NumberFormatStyle, NumberFormatter} from '../facade/intl'; import {NumberWrapper, isBlank, isNumber, isPresent, isString} from '../facade/lang'; import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception'; -var defaultLocale: string = 'en-US'; const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(\-(\d+))?)?$/; function formatNumber( - pipe: Type, value: number | string, style: NumberFormatStyle, digits: string, - currency: string = null, currencyAsSymbol: boolean = false): string { + pipe: Type, locale: string, value: number | string, style: NumberFormatStyle, + digits: string, currency: string = null, currencyAsSymbol: boolean = false): string { if (isBlank(value)) return null; // Convert strings to numbers value = isString(value) && NumberWrapper.isNumeric(value) ? +value : value; @@ -50,7 +49,7 @@ function formatNumber( maxFraction = NumberWrapper.parseIntAutoRadix(parts[5]); } } - return NumberFormatter.format(value as number, defaultLocale, style, { + return NumberFormatter.format(value as number, locale, style, { minimumIntegerDigits: minInt, minimumFractionDigits: minFraction, maximumFractionDigits: maxFraction, @@ -90,8 +89,10 @@ function formatNumber( */ @Pipe({name: 'number'}) export class DecimalPipe implements PipeTransform { + constructor(@Inject(LOCALE_ID) private _locale: string) {} + transform(value: any, digits: string = null): string { - return formatNumber(DecimalPipe, value, NumberFormatStyle.Decimal, digits); + return formatNumber(DecimalPipe, this._locale, value, NumberFormatStyle.Decimal, digits); } } @@ -116,8 +117,10 @@ export class DecimalPipe implements PipeTransform { */ @Pipe({name: 'percent'}) export class PercentPipe implements PipeTransform { + constructor(@Inject(LOCALE_ID) private _locale: string) {} + transform(value: any, digits: string = null): string { - return formatNumber(PercentPipe, value, NumberFormatStyle.Percent, digits); + return formatNumber(PercentPipe, this._locale, value, NumberFormatStyle.Percent, digits); } } @@ -147,10 +150,13 @@ export class PercentPipe implements PipeTransform { */ @Pipe({name: 'currency'}) export class CurrencyPipe implements PipeTransform { + constructor(@Inject(LOCALE_ID) private _locale: string) {} + transform( value: any, currencyCode: string = 'USD', symbolDisplay: boolean = false, digits: string = null): string { return formatNumber( - CurrencyPipe, value, NumberFormatStyle.Currency, digits, currencyCode, symbolDisplay); + CurrencyPipe, this._locale, value, NumberFormatStyle.Currency, digits, currencyCode, + symbolDisplay); } } diff --git a/modules/@angular/common/test/pipes/date_pipe_spec.ts b/modules/@angular/common/test/pipes/date_pipe_spec.ts index 5b10845b4f..09e359641b 100644 --- a/modules/@angular/common/test/pipes/date_pipe_spec.ts +++ b/modules/@angular/common/test/pipes/date_pipe_spec.ts @@ -20,7 +20,7 @@ export function main() { beforeEach(() => { date = DateWrapper.create(2015, 6, 15, 9, 3, 1); - pipe = new DatePipe(); + pipe = new DatePipe('en-US'); }); it('should be marked as pure', diff --git a/modules/@angular/common/test/pipes/number_pipe_spec.ts b/modules/@angular/common/test/pipes/number_pipe_spec.ts index bb26effe22..b52812f057 100644 --- a/modules/@angular/common/test/pipes/number_pipe_spec.ts +++ b/modules/@angular/common/test/pipes/number_pipe_spec.ts @@ -19,7 +19,7 @@ export function main() { describe('DecimalPipe', () => { var pipe: DecimalPipe; - beforeEach(() => { pipe = new DecimalPipe(); }); + beforeEach(() => { pipe = new DecimalPipe('en-US'); }); describe('transform', () => { it('should return correct value for numbers', () => { @@ -50,7 +50,7 @@ export function main() { describe('PercentPipe', () => { var pipe: PercentPipe; - beforeEach(() => { pipe = new PercentPipe(); }); + beforeEach(() => { pipe = new PercentPipe('en-US'); }); describe('transform', () => { it('should return correct value for numbers', () => { @@ -66,7 +66,7 @@ export function main() { describe('CurrencyPipe', () => { var pipe: CurrencyPipe; - beforeEach(() => { pipe = new CurrencyPipe(); }); + beforeEach(() => { pipe = new CurrencyPipe('en-US'); }); describe('transform', () => { it('should return correct value for numbers', () => { diff --git a/tools/public_api_guard/common/index.d.ts b/tools/public_api_guard/common/index.d.ts index ceae67d981..83ba08ee01 100644 --- a/tools/public_api_guard/common/index.d.ts +++ b/tools/public_api_guard/common/index.d.ts @@ -14,16 +14,19 @@ export declare class CommonModule { /** @stable */ export declare class CurrencyPipe implements PipeTransform { + constructor(_locale: string); transform(value: any, currencyCode?: string, symbolDisplay?: boolean, digits?: string): string; } /** @stable */ export declare class DatePipe implements PipeTransform { + constructor(_locale: string); transform(value: any, pattern?: string): string; } /** @stable */ export declare class DecimalPipe implements PipeTransform { + constructor(_locale: string); transform(value: any, digits?: string): string; } @@ -196,6 +199,7 @@ export declare class PathLocationStrategy extends LocationStrategy { /** @stable */ export declare class PercentPipe implements PipeTransform { + constructor(_locale: string); transform(value: any, digits?: string): string; }