fix(i18n): Currency/Date/Number pipe use injected locale (#11093)
This commit is contained in:
parent
4d7d2a2daa
commit
0a053a4cd5
|
@ -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 = <string>StringMapWrapper.get(DatePipe._ALIASES, pattern);
|
||||
}
|
||||
return DateFormatter.format(value, defaultLocale, pattern);
|
||||
return DateFormatter.format(value, this._locale, pattern);
|
||||
}
|
||||
|
||||
private supports(obj: any): boolean {
|
||||
|
|
|
@ -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<any>, value: number | string, style: NumberFormatStyle, digits: string,
|
||||
currency: string = null, currencyAsSymbol: boolean = false): string {
|
||||
pipe: Type<any>, 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue