feat: i18n - include currency code in locale data (#32584)

PR Close #32584
This commit is contained in:
Pete Bacon Darwin 2019-12-14 10:39:41 +00:00 committed by atscott
parent ca1bc7e80b
commit ed2f5e0efa
4 changed files with 49 additions and 18 deletions

View File

@ -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,

View File

@ -44,6 +44,7 @@ export default [
['{1}, {0}', u, '{1} \'at\' {0}', u],
['.', ',', ';', '%', '+', '-', 'E', '×', '‰', '∞', 'NaN', ':'],
['#,##0.###', '#,##0%', '¤#,##0.00', '#E0'],
'USD',
'$',
'US Dollar',
{},

View File

@ -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'); });
});
});
}

View File

@ -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;