mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(ivy): set LOCALE_ID when using the injector (#31983)
In `BrowserModule` the value of `LOCALE_ID` is defined in the `APPLICATION_MODULE_PROVIDERS` after `APP_INITIALIZER` has run. This PR ensures that `LOCALE_ID` is also set for ivy at the same moment which allows the application to fetch the locale from a backend (for example). Fixes #31465 FW-1436 #resolve PR Close #31983
This commit is contained in:
committed by
Alex Rickabaugh
parent
a48376b26a
commit
cfaefa9964
@@ -15,6 +15,7 @@ import localeHu from '@angular/common/locales/hu';
|
||||
import localeSr from '@angular/common/locales/sr';
|
||||
import localeTh from '@angular/common/locales/th';
|
||||
import {isDate, toDate, formatDate} from '@angular/common/src/i18n/format_date';
|
||||
import {ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID} from '@angular/core';
|
||||
|
||||
describe('Format date', () => {
|
||||
describe('toDate', () => {
|
||||
@@ -46,13 +47,12 @@ describe('Format date', () => {
|
||||
|
||||
describe('formatDate', () => {
|
||||
const isoStringWithoutTime = '2015-01-01';
|
||||
const defaultLocale = 'en-US';
|
||||
const defaultFormat = 'mediumDate';
|
||||
let date: Date;
|
||||
|
||||
// Check the transformation of a date into a pattern
|
||||
function expectDateFormatAs(date: Date | string, pattern: any, output: string): void {
|
||||
expect(formatDate(date, pattern, defaultLocale)).toEqual(output, `pattern: "${pattern}"`);
|
||||
expect(formatDate(date, pattern, DEFAULT_LOCALE_ID)).toEqual(output, `pattern: "${pattern}"`);
|
||||
}
|
||||
|
||||
beforeAll(() => {
|
||||
@@ -209,7 +209,8 @@ describe('Format date', () => {
|
||||
};
|
||||
|
||||
Object.keys(dateFixtures).forEach((pattern: string) => {
|
||||
expect(formatDate(date, pattern, defaultLocale, '+0430')).toMatch(dateFixtures[pattern]);
|
||||
expect(formatDate(date, pattern, DEFAULT_LOCALE_ID, '+0430'))
|
||||
.toMatch(dateFixtures[pattern]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -253,22 +254,22 @@ describe('Format date', () => {
|
||||
};
|
||||
|
||||
Object.keys(dateFixtures).forEach((pattern: string) => {
|
||||
expect(formatDate(date, pattern, defaultLocale)).toMatch(dateFixtures[pattern]);
|
||||
expect(formatDate(date, pattern, DEFAULT_LOCALE_ID)).toMatch(dateFixtures[pattern]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should format invalid in IE ISO date',
|
||||
() => expect(formatDate('2017-01-11T12:00:00.014-0500', defaultFormat, defaultLocale))
|
||||
() => expect(formatDate('2017-01-11T12:00:00.014-0500', defaultFormat, DEFAULT_LOCALE_ID))
|
||||
.toEqual('Jan 11, 2017'));
|
||||
|
||||
it('should format invalid in Safari ISO date',
|
||||
() => expect(formatDate('2017-01-20T12:00:00+0000', defaultFormat, defaultLocale))
|
||||
() => expect(formatDate('2017-01-20T12:00:00+0000', defaultFormat, DEFAULT_LOCALE_ID))
|
||||
.toEqual('Jan 20, 2017'));
|
||||
|
||||
// https://github.com/angular/angular/issues/9524
|
||||
// https://github.com/angular/angular/issues/9524
|
||||
it('should format correctly with iso strings that contain time',
|
||||
() => expect(formatDate('2017-05-07T22:14:39', 'dd-MM-yyyy HH:mm', defaultLocale))
|
||||
() => expect(formatDate('2017-05-07T22:14:39', 'dd-MM-yyyy HH:mm', DEFAULT_LOCALE_ID))
|
||||
.toMatch(/07-05-2017 \d{2}:\d{2}/));
|
||||
|
||||
// https://github.com/angular/angular/issues/21491
|
||||
@@ -276,22 +277,22 @@ describe('Format date', () => {
|
||||
// this test only works if the timezone is not in UTC
|
||||
// which is the case for BrowserStack when we test Safari
|
||||
if (new Date().getTimezoneOffset() !== 0) {
|
||||
expect(formatDate('2018-01-11T13:00:00', 'HH', defaultLocale))
|
||||
.not.toEqual(formatDate('2018-01-11T13:00:00Z', 'HH', defaultLocale));
|
||||
expect(formatDate('2018-01-11T13:00:00', 'HH', DEFAULT_LOCALE_ID))
|
||||
.not.toEqual(formatDate('2018-01-11T13:00:00Z', 'HH', DEFAULT_LOCALE_ID));
|
||||
}
|
||||
});
|
||||
|
||||
// https://github.com/angular/angular/issues/16624
|
||||
// https://github.com/angular/angular/issues/17478
|
||||
it('should show the correct time when the timezone is fixed', () => {
|
||||
expect(formatDate('2017-06-13T10:14:39+0000', 'shortTime', defaultLocale, '+0000'))
|
||||
expect(formatDate('2017-06-13T10:14:39+0000', 'shortTime', DEFAULT_LOCALE_ID, '+0000'))
|
||||
.toEqual('10:14 AM');
|
||||
expect(formatDate('2017-06-13T10:14:39+0000', 'h:mm a', defaultLocale, '+0000'))
|
||||
expect(formatDate('2017-06-13T10:14:39+0000', 'h:mm a', DEFAULT_LOCALE_ID, '+0000'))
|
||||
.toEqual('10:14 AM');
|
||||
});
|
||||
|
||||
it('should remove bidi control characters',
|
||||
() => expect(formatDate(date, 'MM/dd/yyyy', defaultLocale) !.length).toEqual(10));
|
||||
() => expect(formatDate(date, 'MM/dd/yyyy', DEFAULT_LOCALE_ID) !.length).toEqual(10));
|
||||
|
||||
it(`should format the date correctly in various locales`, () => {
|
||||
expect(formatDate(date, 'short', 'de')).toEqual('15.06.15, 09:03');
|
||||
|
||||
@@ -12,10 +12,9 @@ import localeFr from '@angular/common/locales/fr';
|
||||
import localeAr from '@angular/common/locales/ar';
|
||||
import {formatCurrency, formatNumber, formatPercent, registerLocaleData} from '@angular/common';
|
||||
import {describe, expect, it} from '@angular/core/testing/src/testing_internal';
|
||||
import {ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID} from '@angular/core';
|
||||
|
||||
describe('Format number', () => {
|
||||
const defaultLocale = 'en-US';
|
||||
|
||||
beforeAll(() => {
|
||||
registerLocaleData(localeEn);
|
||||
registerLocaleData(localeEsUS);
|
||||
@@ -26,18 +25,18 @@ describe('Format number', () => {
|
||||
describe('Number', () => {
|
||||
describe('transform', () => {
|
||||
it('should return correct value for numbers', () => {
|
||||
expect(formatNumber(12345, defaultLocale)).toEqual('12,345');
|
||||
expect(formatNumber(123, defaultLocale, '.2')).toEqual('123.00');
|
||||
expect(formatNumber(1, defaultLocale, '3.')).toEqual('001');
|
||||
expect(formatNumber(1.1, defaultLocale, '3.4-5')).toEqual('001.1000');
|
||||
expect(formatNumber(1.123456, defaultLocale, '3.4-5')).toEqual('001.12346');
|
||||
expect(formatNumber(1.1234, defaultLocale)).toEqual('1.123');
|
||||
expect(formatNumber(1.123456, defaultLocale, '.2')).toEqual('1.123');
|
||||
expect(formatNumber(1.123456, defaultLocale, '.4')).toEqual('1.1235');
|
||||
expect(formatNumber(12345, DEFAULT_LOCALE_ID)).toEqual('12,345');
|
||||
expect(formatNumber(123, DEFAULT_LOCALE_ID, '.2')).toEqual('123.00');
|
||||
expect(formatNumber(1, DEFAULT_LOCALE_ID, '3.')).toEqual('001');
|
||||
expect(formatNumber(1.1, DEFAULT_LOCALE_ID, '3.4-5')).toEqual('001.1000');
|
||||
expect(formatNumber(1.123456, DEFAULT_LOCALE_ID, '3.4-5')).toEqual('001.12346');
|
||||
expect(formatNumber(1.1234, DEFAULT_LOCALE_ID)).toEqual('1.123');
|
||||
expect(formatNumber(1.123456, DEFAULT_LOCALE_ID, '.2')).toEqual('1.123');
|
||||
expect(formatNumber(1.123456, DEFAULT_LOCALE_ID, '.4')).toEqual('1.1235');
|
||||
});
|
||||
|
||||
it('should throw if minFractionDigits is explicitly higher than maxFractionDigits', () => {
|
||||
expect(() => formatNumber(1.1, defaultLocale, '3.4-2'))
|
||||
expect(() => formatNumber(1.1, DEFAULT_LOCALE_ID, '3.4-2'))
|
||||
.toThrowError(/is higher than the maximum/);
|
||||
});
|
||||
});
|
||||
@@ -51,27 +50,27 @@ describe('Format number', () => {
|
||||
describe('Percent', () => {
|
||||
describe('transform', () => {
|
||||
it('should return correct value for numbers', () => {
|
||||
expect(formatPercent(1.23, defaultLocale)).toEqual('123%');
|
||||
expect(formatPercent(1.2, defaultLocale, '.2')).toEqual('120.00%');
|
||||
expect(formatPercent(1.2, defaultLocale, '4.2')).toEqual('0,120.00%');
|
||||
expect(formatPercent(1.23, DEFAULT_LOCALE_ID)).toEqual('123%');
|
||||
expect(formatPercent(1.2, DEFAULT_LOCALE_ID, '.2')).toEqual('120.00%');
|
||||
expect(formatPercent(1.2, DEFAULT_LOCALE_ID, '4.2')).toEqual('0,120.00%');
|
||||
expect(formatPercent(1.2, 'fr', '4.2')).toEqual('0 120,00 %');
|
||||
expect(formatPercent(1.2, 'ar', '4.2')).toEqual('0,120.00%');
|
||||
// see issue #20136
|
||||
expect(formatPercent(0.12345674, defaultLocale, '0.0-10')).toEqual('12.345674%');
|
||||
expect(formatPercent(0, defaultLocale, '0.0-10')).toEqual('0%');
|
||||
expect(formatPercent(0.00, defaultLocale, '0.0-10')).toEqual('0%');
|
||||
expect(formatPercent(1, defaultLocale, '0.0-10')).toEqual('100%');
|
||||
expect(formatPercent(0.1, defaultLocale, '0.0-10')).toEqual('10%');
|
||||
expect(formatPercent(0.12, defaultLocale, '0.0-10')).toEqual('12%');
|
||||
expect(formatPercent(0.123, defaultLocale, '0.0-10')).toEqual('12.3%');
|
||||
expect(formatPercent(12.3456, defaultLocale, '0.0-10')).toEqual('1,234.56%');
|
||||
expect(formatPercent(12.345600, defaultLocale, '0.0-10')).toEqual('1,234.56%');
|
||||
expect(formatPercent(12.345699999, defaultLocale, '0.0-6')).toEqual('1,234.57%');
|
||||
expect(formatPercent(12.345699999, defaultLocale, '0.4-6')).toEqual('1,234.5700%');
|
||||
expect(formatPercent(100, defaultLocale, '0.4-6')).toEqual('10,000.0000%');
|
||||
expect(formatPercent(100, defaultLocale, '0.0-10')).toEqual('10,000%');
|
||||
expect(formatPercent(1.5e2, defaultLocale)).toEqual('15,000%');
|
||||
expect(formatPercent(1e100, defaultLocale)).toEqual('1E+102%');
|
||||
expect(formatPercent(0.12345674, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('12.345674%');
|
||||
expect(formatPercent(0, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('0%');
|
||||
expect(formatPercent(0.00, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('0%');
|
||||
expect(formatPercent(1, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('100%');
|
||||
expect(formatPercent(0.1, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('10%');
|
||||
expect(formatPercent(0.12, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('12%');
|
||||
expect(formatPercent(0.123, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('12.3%');
|
||||
expect(formatPercent(12.3456, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('1,234.56%');
|
||||
expect(formatPercent(12.345600, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('1,234.56%');
|
||||
expect(formatPercent(12.345699999, DEFAULT_LOCALE_ID, '0.0-6')).toEqual('1,234.57%');
|
||||
expect(formatPercent(12.345699999, DEFAULT_LOCALE_ID, '0.4-6')).toEqual('1,234.5700%');
|
||||
expect(formatPercent(100, DEFAULT_LOCALE_ID, '0.4-6')).toEqual('10,000.0000%');
|
||||
expect(formatPercent(100, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('10,000%');
|
||||
expect(formatPercent(1.5e2, DEFAULT_LOCALE_ID)).toEqual('15,000%');
|
||||
expect(formatPercent(1e100, DEFAULT_LOCALE_ID)).toEqual('1E+102%');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -80,16 +79,16 @@ describe('Format number', () => {
|
||||
const defaultCurrencyCode = 'USD';
|
||||
describe('transform', () => {
|
||||
it('should return correct value for numbers', () => {
|
||||
expect(formatCurrency(123, defaultLocale, '$')).toEqual('$123.00');
|
||||
expect(formatCurrency(12, defaultLocale, 'EUR', 'EUR', '.1')).toEqual('EUR12.0');
|
||||
expect(
|
||||
formatCurrency(5.1234, defaultLocale, defaultCurrencyCode, defaultCurrencyCode, '.0-3'))
|
||||
expect(formatCurrency(123, DEFAULT_LOCALE_ID, '$')).toEqual('$123.00');
|
||||
expect(formatCurrency(12, DEFAULT_LOCALE_ID, 'EUR', 'EUR', '.1')).toEqual('EUR12.0');
|
||||
expect(formatCurrency(
|
||||
5.1234, DEFAULT_LOCALE_ID, defaultCurrencyCode, defaultCurrencyCode, '.0-3'))
|
||||
.toEqual('USD5.123');
|
||||
expect(formatCurrency(5.1234, defaultLocale, defaultCurrencyCode)).toEqual('USD5.12');
|
||||
expect(formatCurrency(5.1234, defaultLocale, '$')).toEqual('$5.12');
|
||||
expect(formatCurrency(5.1234, defaultLocale, 'CA$')).toEqual('CA$5.12');
|
||||
expect(formatCurrency(5.1234, defaultLocale, '$')).toEqual('$5.12');
|
||||
expect(formatCurrency(5.1234, defaultLocale, '$', defaultCurrencyCode, '5.2-2'))
|
||||
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, defaultCurrencyCode)).toEqual('USD5.12');
|
||||
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, '$')).toEqual('$5.12');
|
||||
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'CA$')).toEqual('CA$5.12');
|
||||
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, '$')).toEqual('$5.12');
|
||||
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, '$', defaultCurrencyCode, '5.2-2'))
|
||||
.toEqual('$00,005.12');
|
||||
expect(formatCurrency(5.1234, 'fr', '$', defaultCurrencyCode, '5.2-2'))
|
||||
.toEqual('00 005,12 $');
|
||||
@@ -98,20 +97,21 @@ describe('Format number', () => {
|
||||
|
||||
it('should support any currency code name', () => {
|
||||
// currency code is unknown, default formatting options will be used
|
||||
expect(formatCurrency(5.1234, defaultLocale, 'unexisting_ISO_code'))
|
||||
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'unexisting_ISO_code'))
|
||||
.toEqual('unexisting_ISO_code5.12');
|
||||
// currency code is USD, the pipe will format based on USD but will display "Custom name"
|
||||
expect(formatCurrency(5.1234, defaultLocale, 'Custom name')).toEqual('Custom name5.12');
|
||||
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'Custom name')).toEqual('Custom name5.12');
|
||||
});
|
||||
|
||||
it('should round to the default number of digits if no digitsInfo', () => {
|
||||
// IDR has a default number of digits of 0
|
||||
expect(formatCurrency(5.1234, defaultLocale, 'IDR', 'IDR')).toEqual('IDR5');
|
||||
expect(formatCurrency(5.1234, defaultLocale, 'IDR', 'IDR', '.2')).toEqual('IDR5.12');
|
||||
expect(formatCurrency(5.1234, defaultLocale, 'Custom name', 'IDR')).toEqual('Custom name5');
|
||||
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'IDR', 'IDR')).toEqual('IDR5');
|
||||
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'IDR', 'IDR', '.2')).toEqual('IDR5.12');
|
||||
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'Custom name', 'IDR'))
|
||||
.toEqual('Custom name5');
|
||||
// BHD has a default number of digits of 3
|
||||
expect(formatCurrency(5.1234, defaultLocale, 'BHD', 'BHD')).toEqual('BHD5.123');
|
||||
expect(formatCurrency(5.1234, defaultLocale, 'BHD', 'BHD', '.1-2')).toEqual('BHD5.12');
|
||||
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'BHD', 'BHD')).toEqual('BHD5.123');
|
||||
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'BHD', 'BHD', '.1-2')).toEqual('BHD5.12');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user