feat: i18n - include currency code in locale data (#32584)
PR Close #32584
This commit is contained in:
parent
ca1bc7e80b
commit
ed2f5e0efa
|
@ -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,
|
||||
|
|
|
@ -44,6 +44,7 @@ export default [
|
|||
['{1}, {0}', u, '{1} \'at\' {0}', u],
|
||||
['.', ',', ';', '%', '+', '-', 'E', '×', '‰', '∞', 'NaN', ':'],
|
||||
['#,##0.###', '#,##0%', '¤#,##0.00', '#E0'],
|
||||
'USD',
|
||||
'$',
|
||||
'US Dollar',
|
||||
{},
|
||||
|
|
|
@ -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'); });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -149,12 +149,15 @@ function generateGlobalLocale(locale, localeData, baseCurrencies) {
|
|||
* Collect up the basic locale data [ localeId, dateTime, number, currency, pluralCase ].
|
||||
*/
|
||||
function generateBasicLocaleString(locale, localeData, baseCurrencies) {
|
||||
let data =
|
||||
stringify(
|
||||
let data = stringify(
|
||||
[
|
||||
locale, ...getDateTimeTranslations(localeData), ...getDateTimeSettings(localeData),
|
||||
...getNumberSettings(localeData), ...getCurrencySettings(locale, localeData),
|
||||
generateLocaleCurrencies(localeData, baseCurrencies), getDirectionality(localeData),
|
||||
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
|
||||
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue