Skip to content

Commit 879756d

Browse files
trotylmhevery
authored andcommitted
fix(common): fallback to last defined value for named date and time formats (#21299)
closes #21282 PR Close #21299
1 parent 2cf9d6d commit 879756d

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

packages/common/src/i18n/locale_data_api.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export function getLocaleWeekEndRange(locale: string): [WeekDay, WeekDay] {
253253
*/
254254
export function getLocaleDateFormat(locale: string, width: FormatWidth): string {
255255
const data = findLocaleData(locale);
256-
return data[LocaleDataIndex.DateFormat][width];
256+
return getLastDefinedValue(data[LocaleDataIndex.DateFormat], width);
257257
}
258258

259259
/**
@@ -278,7 +278,7 @@ export function getLocaleDateFormat(locale: string, width: FormatWidth): string
278278
*/
279279
export function getLocaleTimeFormat(locale: string, width: FormatWidth): string {
280280
const data = findLocaleData(locale);
281-
return data[LocaleDataIndex.TimeFormat][width];
281+
return getLastDefinedValue(data[LocaleDataIndex.TimeFormat], width);
282282
}
283283

284284
/**

packages/common/test/i18n/locale_data_api_spec.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
import localeCaESVALENCIA from '@angular/common/locales/ca-ES-VALENCIA';
1010
import localeEn from '@angular/common/locales/en';
1111
import localeFr from '@angular/common/locales/fr';
12+
import localeZh from '@angular/common/locales/zh';
1213
import localeFrCA from '@angular/common/locales/fr-CA';
1314
import {registerLocaleData} from '../../src/i18n/locale_data';
14-
import {findLocaleData, getCurrencySymbol} from '../../src/i18n/locale_data_api';
15+
import {findLocaleData, getCurrencySymbol, getLocaleDateFormat, FormatWidth} from '../../src/i18n/locale_data_api';
1516

1617
{
1718
describe('locale data api', () => {
@@ -22,6 +23,7 @@ import {findLocaleData, getCurrencySymbol} from '../../src/i18n/locale_data_api'
2223
registerLocaleData(localeFrCA);
2324
registerLocaleData(localeFr, 'fake-id');
2425
registerLocaleData(localeFrCA, 'fake_Id2');
26+
registerLocaleData(localeZh);
2527
});
2628

2729
describe('findLocaleData', () => {
@@ -64,5 +66,10 @@ import {findLocaleData, getCurrencySymbol} from '../../src/i18n/locale_data_api'
6466
expect(getCurrencySymbol('FAKE', 'narrow')).toEqual('FAKE');
6567
});
6668
});
69+
70+
describe('getLastDefinedValue', () => {
71+
it('should find the last defined date format when format not defined',
72+
() => { expect(getLocaleDateFormat('zh', FormatWidth.Long)).toEqual('y年M月d日'); });
73+
});
6774
});
6875
}

0 commit comments

Comments
 (0)