fix(i18n): Currency/Date/Number pipe use injected locale (#11093)

This commit is contained in:
Victor Berchet 2016-08-26 09:16:01 -07:00 committed by GitHub
parent 4d7d2a2daa
commit 0a053a4cd5
5 changed files with 27 additions and 16 deletions

View File

@ -6,14 +6,14 @@
* found in the LICENSE file at https://angular.io/license * 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 {StringMapWrapper} from '../facade/collection';
import {DateFormatter} from '../facade/intl'; import {DateFormatter} from '../facade/intl';
import {DateWrapper, NumberWrapper, isBlank, isDate, isString} from '../facade/lang'; import {DateWrapper, NumberWrapper, isBlank, isDate, isString} from '../facade/lang';
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception'; 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. * Formats a date value to a string based on the requested format.
@ -96,6 +96,7 @@ export class DatePipe implements PipeTransform {
'shortTime': 'jm' 'shortTime': 'jm'
}; };
constructor(@Inject(LOCALE_ID) private _locale: string) {}
transform(value: any, pattern: string = 'mediumDate'): string { transform(value: any, pattern: string = 'mediumDate'): string {
if (isBlank(value)) return null; if (isBlank(value)) return null;
@ -112,7 +113,7 @@ export class DatePipe implements PipeTransform {
if (StringMapWrapper.contains(DatePipe._ALIASES, pattern)) { if (StringMapWrapper.contains(DatePipe._ALIASES, pattern)) {
pattern = <string>StringMapWrapper.get(DatePipe._ALIASES, pattern); pattern = <string>StringMapWrapper.get(DatePipe._ALIASES, pattern);
} }
return DateFormatter.format(value, defaultLocale, pattern); return DateFormatter.format(value, this._locale, pattern);
} }
private supports(obj: any): boolean { private supports(obj: any): boolean {

View File

@ -6,19 +6,18 @@
* found in the LICENSE file at https://angular.io/license * 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 {NumberFormatStyle, NumberFormatter} from '../facade/intl';
import {NumberWrapper, isBlank, isNumber, isPresent, isString} from '../facade/lang'; import {NumberWrapper, isBlank, isNumber, isPresent, isString} from '../facade/lang';
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception'; import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
var defaultLocale: string = 'en-US';
const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(\-(\d+))?)?$/; const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(\-(\d+))?)?$/;
function formatNumber( function formatNumber(
pipe: Type<any>, value: number | string, style: NumberFormatStyle, digits: string, pipe: Type<any>, locale: string, value: number | string, style: NumberFormatStyle,
currency: string = null, currencyAsSymbol: boolean = false): string { digits: string, currency: string = null, currencyAsSymbol: boolean = false): string {
if (isBlank(value)) return null; if (isBlank(value)) return null;
// Convert strings to numbers // Convert strings to numbers
value = isString(value) && NumberWrapper.isNumeric(value) ? +value : value; value = isString(value) && NumberWrapper.isNumeric(value) ? +value : value;
@ -50,7 +49,7 @@ function formatNumber(
maxFraction = NumberWrapper.parseIntAutoRadix(parts[5]); maxFraction = NumberWrapper.parseIntAutoRadix(parts[5]);
} }
} }
return NumberFormatter.format(value as number, defaultLocale, style, { return NumberFormatter.format(value as number, locale, style, {
minimumIntegerDigits: minInt, minimumIntegerDigits: minInt,
minimumFractionDigits: minFraction, minimumFractionDigits: minFraction,
maximumFractionDigits: maxFraction, maximumFractionDigits: maxFraction,
@ -90,8 +89,10 @@ function formatNumber(
*/ */
@Pipe({name: 'number'}) @Pipe({name: 'number'})
export class DecimalPipe implements PipeTransform { export class DecimalPipe implements PipeTransform {
constructor(@Inject(LOCALE_ID) private _locale: string) {}
transform(value: any, digits: string = null): 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'}) @Pipe({name: 'percent'})
export class PercentPipe implements PipeTransform { export class PercentPipe implements PipeTransform {
constructor(@Inject(LOCALE_ID) private _locale: string) {}
transform(value: any, digits: string = null): 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'}) @Pipe({name: 'currency'})
export class CurrencyPipe implements PipeTransform { export class CurrencyPipe implements PipeTransform {
constructor(@Inject(LOCALE_ID) private _locale: string) {}
transform( transform(
value: any, currencyCode: string = 'USD', symbolDisplay: boolean = false, value: any, currencyCode: string = 'USD', symbolDisplay: boolean = false,
digits: string = null): string { digits: string = null): string {
return formatNumber( return formatNumber(
CurrencyPipe, value, NumberFormatStyle.Currency, digits, currencyCode, symbolDisplay); CurrencyPipe, this._locale, value, NumberFormatStyle.Currency, digits, currencyCode,
symbolDisplay);
} }
} }

View File

@ -20,7 +20,7 @@ export function main() {
beforeEach(() => { beforeEach(() => {
date = DateWrapper.create(2015, 6, 15, 9, 3, 1); date = DateWrapper.create(2015, 6, 15, 9, 3, 1);
pipe = new DatePipe(); pipe = new DatePipe('en-US');
}); });
it('should be marked as pure', it('should be marked as pure',

View File

@ -19,7 +19,7 @@ export function main() {
describe('DecimalPipe', () => { describe('DecimalPipe', () => {
var pipe: DecimalPipe; var pipe: DecimalPipe;
beforeEach(() => { pipe = new DecimalPipe(); }); beforeEach(() => { pipe = new DecimalPipe('en-US'); });
describe('transform', () => { describe('transform', () => {
it('should return correct value for numbers', () => { it('should return correct value for numbers', () => {
@ -50,7 +50,7 @@ export function main() {
describe('PercentPipe', () => { describe('PercentPipe', () => {
var pipe: PercentPipe; var pipe: PercentPipe;
beforeEach(() => { pipe = new PercentPipe(); }); beforeEach(() => { pipe = new PercentPipe('en-US'); });
describe('transform', () => { describe('transform', () => {
it('should return correct value for numbers', () => { it('should return correct value for numbers', () => {
@ -66,7 +66,7 @@ export function main() {
describe('CurrencyPipe', () => { describe('CurrencyPipe', () => {
var pipe: CurrencyPipe; var pipe: CurrencyPipe;
beforeEach(() => { pipe = new CurrencyPipe(); }); beforeEach(() => { pipe = new CurrencyPipe('en-US'); });
describe('transform', () => { describe('transform', () => {
it('should return correct value for numbers', () => { it('should return correct value for numbers', () => {

View File

@ -14,16 +14,19 @@ export declare class CommonModule {
/** @stable */ /** @stable */
export declare class CurrencyPipe implements PipeTransform { export declare class CurrencyPipe implements PipeTransform {
constructor(_locale: string);
transform(value: any, currencyCode?: string, symbolDisplay?: boolean, digits?: string): string; transform(value: any, currencyCode?: string, symbolDisplay?: boolean, digits?: string): string;
} }
/** @stable */ /** @stable */
export declare class DatePipe implements PipeTransform { export declare class DatePipe implements PipeTransform {
constructor(_locale: string);
transform(value: any, pattern?: string): string; transform(value: any, pattern?: string): string;
} }
/** @stable */ /** @stable */
export declare class DecimalPipe implements PipeTransform { export declare class DecimalPipe implements PipeTransform {
constructor(_locale: string);
transform(value: any, digits?: string): string; transform(value: any, digits?: string): string;
} }
@ -196,6 +199,7 @@ export declare class PathLocationStrategy extends LocationStrategy {
/** @stable */ /** @stable */
export declare class PercentPipe implements PipeTransform { export declare class PercentPipe implements PipeTransform {
constructor(_locale: string);
transform(value: any, digits?: string): string; transform(value: any, digits?: string): string;
} }