diff --git a/packages/core/src/i18n/locale_data_api.ts b/packages/core/src/i18n/locale_data_api.ts index 4b5895637f..65ca68b99d 100644 --- a/packages/core/src/i18n/locale_data_api.ts +++ b/packages/core/src/i18n/locale_data_api.ts @@ -63,6 +63,21 @@ export function findLocaleData(locale: string): any { throw new Error(`Missing locale data for the locale "${locale}".`); } +/** + * Retrieves the default currency code for the given locale. + * + * The default is defined as the first currency which is still in use. + * + * @param locale The code of the locale whose currency code we want. + * @returns The code of the default currency for the given locale. + * + * @publicApi + */ +export function getLocaleCurrencyCode(locale: string): string|null { + const data = findLocaleData(locale); + return data[LocaleDataIndex.CurrencyCode] || null; +} + /** * Retrieves the plural function used by ICU expressions to determine the plural case to use * for a given locale. @@ -116,6 +131,7 @@ export enum LocaleDataIndex { DateTimeFormat, NumberSymbols, NumberFormats, + CurrencyCode, CurrencySymbol, CurrencyName, Currencies, diff --git a/packages/core/src/i18n/locale_en.ts b/packages/core/src/i18n/locale_en.ts index 785c6d08e7..2e430b5575 100644 --- a/packages/core/src/i18n/locale_en.ts +++ b/packages/core/src/i18n/locale_en.ts @@ -44,6 +44,7 @@ export default [ ['{1}, {0}', u, '{1} \'at\' {0}', u], ['.', ',', ';', '%', '+', '-', 'E', '×', '‰', '∞', 'NaN', ':'], ['#,##0.###', '#,##0%', '¤#,##0.00', '#E0'], + 'USD', '$', 'US Dollar', {}, diff --git a/packages/core/test/i18n/locale_data_api_spec.ts b/packages/core/test/i18n/locale_data_api_spec.ts index 9121e3b2cb..3c995a61d3 100644 --- a/packages/core/test/i18n/locale_data_api_spec.ts +++ b/packages/core/test/i18n/locale_data_api_spec.ts @@ -5,7 +5,7 @@ * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ -import {findLocaleData, registerLocaleData, unregisterAllLocaleData} from '../../src/i18n/locale_data_api'; +import {LocaleDataIndex, findLocaleData, getLocaleCurrencyCode, registerLocaleData, unregisterAllLocaleData} from '../../src/i18n/locale_data_api'; import {global} from '../../src/util/global'; { @@ -15,6 +15,7 @@ import {global} from '../../src/util/global'; const localeDeCH: any[] = ['de-CH']; const localeEn: any[] = ['en']; const localeFr: any[] = ['fr']; + localeFr[LocaleDataIndex.CurrencyCode] = 'EUR'; const localeFrCA: any[] = ['fr-CA']; const localeZh: any[] = ['zh']; const localeEnAU: any[] = ['en-AU']; @@ -81,5 +82,13 @@ import {global} from '../../src/util/global'; it('should find the registered LOCALE_DATA even if the same locale is on the global object', () => { expect(findLocaleData('fr')).not.toBe(fakeGlobalFr); }); }); + + describe('getLocaleCurrencyCode()', () => { + it('should return `null` if the given locale does not provide a currency code', + () => { expect(getLocaleCurrencyCode('de')).toBe(null); }); + + it('should return the code at the `LocaleDataIndex.CurrencyCode` of the given locale`s data', + () => { expect(getLocaleCurrencyCode('fr')).toEqual('EUR'); }); + }); }); } diff --git a/tools/gulp-tasks/cldr/extract.js b/tools/gulp-tasks/cldr/extract.js index 81c89e795f..084c50e21c 100644 --- a/tools/gulp-tasks/cldr/extract.js +++ b/tools/gulp-tasks/cldr/extract.js @@ -97,8 +97,8 @@ module.exports = (gulp, done) => { console.log(`All i18n cldr files have been generated, formatting files..."`); shelljs.exec( - `yarn clang-format -i ${I18N_DATA_FOLDER}/**/*.ts ${I18N_DATA_FOLDER}/*.ts ${I18N_FOLDER}/currencies.ts ${I18N_CORE_FOLDER}/locale_en.ts ${I18N_GLOBAL_FOLDER}/*.js`, - {silent: true}); + `yarn clang-format -i ${I18N_DATA_FOLDER}/**/*.ts ${I18N_DATA_FOLDER}/*.ts ${I18N_FOLDER}/currencies.ts ${I18N_CORE_FOLDER}/locale_en.ts ${I18N_GLOBAL_FOLDER}/*.js`, + {silent: true}); done(); }; @@ -149,16 +149,19 @@ function generateGlobalLocale(locale, localeData, baseCurrencies) { * Collect up the basic locale data [ localeId, dateTime, number, currency, pluralCase ]. */ function generateBasicLocaleString(locale, localeData, baseCurrencies) { - let data = - stringify( - [ - locale, ...getDateTimeTranslations(localeData), ...getDateTimeSettings(localeData), - ...getNumberSettings(localeData), ...getCurrencySettings(locale, localeData), - generateLocaleCurrencies(localeData, baseCurrencies), getDirectionality(localeData), - ], - true) - // We remove "undefined" added by spreading arrays when there is no value - .replace(/undefined/g, 'u'); + let data = stringify( + [ + locale, + ...getDateTimeTranslations(localeData), + ...getDateTimeSettings(localeData), + ...getNumberSettings(localeData), + ...getCurrencySettings(locale, localeData), + generateLocaleCurrencies(localeData, baseCurrencies), + getDirectionality(localeData), + ], + true) + // We remove "undefined" added by spreading arrays when there is no value + .replace(/undefined/g, 'u'); // adding plural function after, because we don't want it as a string data = data.replace(/\]$/, ', plural]'); @@ -495,8 +498,8 @@ function getNumberSettings(localeData) { } /** - * Returns the currency symbol and name for a locale - * @returns [ symbol, name ] + * Returns the currency code, symbol and name for a locale + * @returns [ code, symbol, name ] */ function getCurrencySettings(locale, localeData) { const currencyInfo = localeData.main(`numbers/currencies`); @@ -523,11 +526,13 @@ function getCurrencySettings(locale, localeData) { } } - let currencySettings = [undefined, undefined]; + let currencySettings = [undefined, undefined, undefined]; if (currentCurrency) { - currencySettings = - [currencyInfo[currentCurrency].symbol, currencyInfo[currentCurrency].displayName]; + currencySettings = [ + currentCurrency, currencyInfo[currentCurrency].symbol, + currencyInfo[currentCurrency].displayName + ]; } return currencySettings;