diff --git a/packages/common/src/common.ts b/packages/common/src/common.ts index 3391cd21a3..1041b7bf3e 100644 --- a/packages/common/src/common.ts +++ b/packages/common/src/common.ts @@ -14,8 +14,7 @@ export * from './location/index'; export {NgLocaleLocalization, NgLocalization} from './i18n/localization'; export {registerLocaleData} from './i18n/locale_data'; -export {Plural, NumberFormatStyle, FormStyle, Time, TranslationWidth, FormatWidth, NumberSymbol, WeekDay, getLocaleDayPeriods, getLocaleDayNames, getLocaleMonthNames, getLocaleId, getLocaleEraNames, getLocaleWeekEndRange, getLocaleFirstDayOfWeek, getLocaleDateFormat, getLocaleDateTimeFormat, getLocaleExtraDayPeriodRules, getLocaleExtraDayPeriods, getLocalePluralCase, getLocaleTimeFormat, getLocaleNumberSymbol, getLocaleNumberFormat, getLocaleCurrencyName, getLocaleCurrencySymbol} from './i18n/locale_data_api'; -export {CURRENCIES} from './i18n/currencies'; +export {Plural, NumberFormatStyle, FormStyle, Time, TranslationWidth, FormatWidth, NumberSymbol, WeekDay, getCurrencySymbol, getLocaleDayPeriods, getLocaleDayNames, getLocaleMonthNames, getLocaleId, getLocaleEraNames, getLocaleWeekEndRange, getLocaleFirstDayOfWeek, getLocaleDateFormat, getLocaleDateTimeFormat, getLocaleExtraDayPeriodRules, getLocaleExtraDayPeriods, getLocalePluralCase, getLocaleTimeFormat, getLocaleNumberSymbol, getLocaleNumberFormat, getLocaleCurrencyName, getLocaleCurrencySymbol} from './i18n/locale_data_api'; export {parseCookieValue as ɵparseCookieValue} from './cookie'; export {CommonModule, DeprecatedI18NPipesModule} from './common_module'; export {NgClass, NgForOf, NgForOfContext, NgIf, NgIfContext, NgPlural, NgPluralCase, NgStyle, NgSwitch, NgSwitchCase, NgSwitchDefault, NgTemplateOutlet, NgComponentOutlet} from './directives/index'; diff --git a/packages/common/src/i18n/currencies.ts b/packages/common/src/i18n/currencies.ts index a81807aa69..944e1b423b 100644 --- a/packages/common/src/i18n/currencies.ts +++ b/packages/common/src/i18n/currencies.ts @@ -9,7 +9,7 @@ // THIS CODE IS GENERATED - DO NOT MODIFY // See angular/tools/gulp-tasks/cldr/extract.js -/** @experimental */ +/** @internal */ export const CURRENCIES: {[code: string]: (string | undefined)[]} = { 'AOA': [, 'Kz'], 'ARS': [, '$'], diff --git a/packages/common/src/i18n/locale_data_api.ts b/packages/common/src/i18n/locale_data_api.ts index 6cd27145c3..9e5ecb0ac7 100644 --- a/packages/common/src/i18n/locale_data_api.ts +++ b/packages/common/src/i18n/locale_data_api.ts @@ -527,12 +527,17 @@ export function findLocaleData(locale: string): any { /** * Return the currency symbol for a given currency code, or the code if no symbol available - * (e.g.: $, US$, or USD) + * (e.g.: format narrow = $, format wide = US$, code = USD) * - * @internal + * @experimental i18n support is experimental. */ -export function findCurrencySymbol(code: string, format: 'wide' | 'narrow') { - const currency = CURRENCIES[code] || {}; - const symbol = currency[0] || code; - return format === 'wide' ? symbol : currency[1] || symbol; -} \ No newline at end of file +export function getCurrencySymbol(code: string, format: 'wide' | 'narrow'): string { + const currency = CURRENCIES[code] || []; + const symbolNarrow = currency[1]; + + if (format === 'narrow' && typeof symbolNarrow === 'string') { + return symbolNarrow; + } + + return currency[0] || code; +} diff --git a/packages/common/src/pipes/number_pipe.ts b/packages/common/src/pipes/number_pipe.ts index 130e3a5384..bb4616c29a 100644 --- a/packages/common/src/pipes/number_pipe.ts +++ b/packages/common/src/pipes/number_pipe.ts @@ -8,7 +8,7 @@ import {Inject, LOCALE_ID, Pipe, PipeTransform} from '@angular/core'; import {formatNumber} from '../i18n/format_number'; -import {NumberFormatStyle, findCurrencySymbol, getLocaleCurrencyName, getLocaleCurrencySymbol} from '../i18n/locale_data_api'; +import {NumberFormatStyle, getCurrencySymbol, getLocaleCurrencyName, getLocaleCurrencySymbol} from '../i18n/locale_data_api'; import {invalidPipeArgumentError} from './invalid_pipe_argument_error'; /** @@ -143,7 +143,7 @@ export class CurrencyPipe implements PipeTransform { let currency = currencyCode || 'USD'; if (display !== 'code') { - currency = findCurrencySymbol(currency, display === 'symbol' ? 'wide' : 'narrow'); + currency = getCurrencySymbol(currency, display === 'symbol' ? 'wide' : 'narrow'); } const {str, error} = formatNumber(value, locale, NumberFormatStyle.Currency, digits, currency); diff --git a/packages/common/test/i18n/locale_data_api_spec.ts b/packages/common/test/i18n/locale_data_api_spec.ts index 667977e82d..911a4cc61d 100644 --- a/packages/common/test/i18n/locale_data_api_spec.ts +++ b/packages/common/test/i18n/locale_data_api_spec.ts @@ -11,7 +11,7 @@ import localeEn from '@angular/common/locales/en'; import localeFr from '@angular/common/locales/fr'; import localeFrCA from '@angular/common/locales/fr-CA'; import {registerLocaleData} from '../../src/i18n/locale_data'; -import {findLocaleData} from '../../src/i18n/locale_data_api'; +import {findLocaleData, getCurrencySymbol} from '../../src/i18n/locale_data_api'; { describe('locale data api', () => { @@ -51,5 +51,18 @@ import {findLocaleData} from '../../src/i18n/locale_data_api'; expect(findLocaleData('fake-id2')).toEqual(localeFrCA); }); }); + + describe('getCurrencySymbolElseCode', () => { + it('should return the correct symbol', () => { + expect(getCurrencySymbol('USD', 'wide')).toEqual('$'); + expect(getCurrencySymbol('USD', 'narrow')).toEqual('$'); + expect(getCurrencySymbol('AUD', 'wide')).toEqual('A$'); + expect(getCurrencySymbol('AUD', 'narrow')).toEqual('$'); + expect(getCurrencySymbol('CRC', 'wide')).toEqual('CRC'); + expect(getCurrencySymbol('CRC', 'narrow')).toEqual('₡'); + expect(getCurrencySymbol('FAKE', 'wide')).toEqual('FAKE'); + expect(getCurrencySymbol('FAKE', 'narrow')).toEqual('FAKE'); + }); + }); }); } diff --git a/packages/common/test/pipes/number_pipe_spec.ts b/packages/common/test/pipes/number_pipe_spec.ts index f4b6d83bb4..abc0eb6c1a 100644 --- a/packages/common/test/pipes/number_pipe_spec.ts +++ b/packages/common/test/pipes/number_pipe_spec.ts @@ -122,6 +122,7 @@ import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testin expect(pipe.transform(5.1234, 'CAD', 'symbol-narrow', '5.2-2')).toEqual('$00,005.12'); expect(pipe.transform(5.1234, 'CAD', 'symbol-narrow', '5.2-2', 'fr')) .toEqual('00 005,12 $'); + expect(pipe.transform(5.1234, 'FAKE', 'symbol')).toEqual('FAKE5.12'); }); it('should not support other objects', () => { diff --git a/tools/public_api_guard/common/common.d.ts b/tools/public_api_guard/common/common.d.ts index 07ccaf97dd..40d879e2ea 100644 --- a/tools/public_api_guard/common/common.d.ts +++ b/tools/public_api_guard/common/common.d.ts @@ -15,11 +15,6 @@ export declare class AsyncPipe implements OnDestroy, PipeTransform { export declare class CommonModule { } -/** @experimental */ -export declare const CURRENCIES: { - [code: string]: (string | undefined)[]; -}; - /** @stable */ export declare class CurrencyPipe implements PipeTransform { constructor(_locale: string); @@ -83,6 +78,9 @@ export declare enum FormStyle { Standalone = 1, } +/** @experimental */ +export declare function getCurrencySymbol(code: string, format: 'wide' | 'narrow'): string; + /** @experimental */ export declare function getLocaleCurrencyName(locale: string): string | null;