diff --git a/packages/common/src/i18n/locale_data_api.ts b/packages/common/src/i18n/locale_data_api.ts index 9e5ecb0ac7..4bf240e497 100644 --- a/packages/common/src/i18n/locale_data_api.ts +++ b/packages/common/src/i18n/locale_data_api.ts @@ -253,7 +253,7 @@ export function getLocaleWeekEndRange(locale: string): [WeekDay, WeekDay] { */ export function getLocaleDateFormat(locale: string, width: FormatWidth): string { const data = findLocaleData(locale); - return data[LocaleDataIndex.DateFormat][width]; + return getLastDefinedValue(data[LocaleDataIndex.DateFormat], width); } /** @@ -278,7 +278,7 @@ export function getLocaleDateFormat(locale: string, width: FormatWidth): string */ export function getLocaleTimeFormat(locale: string, width: FormatWidth): string { const data = findLocaleData(locale); - return data[LocaleDataIndex.TimeFormat][width]; + return getLastDefinedValue(data[LocaleDataIndex.TimeFormat], width); } /** diff --git a/packages/common/test/i18n/locale_data_api_spec.ts b/packages/common/test/i18n/locale_data_api_spec.ts index 911a4cc61d..ad7b673bf7 100644 --- a/packages/common/test/i18n/locale_data_api_spec.ts +++ b/packages/common/test/i18n/locale_data_api_spec.ts @@ -9,9 +9,10 @@ import localeCaESVALENCIA from '@angular/common/locales/ca-ES-VALENCIA'; import localeEn from '@angular/common/locales/en'; import localeFr from '@angular/common/locales/fr'; +import localeZh from '@angular/common/locales/zh'; import localeFrCA from '@angular/common/locales/fr-CA'; import {registerLocaleData} from '../../src/i18n/locale_data'; -import {findLocaleData, getCurrencySymbol} from '../../src/i18n/locale_data_api'; +import {findLocaleData, getCurrencySymbol, getLocaleDateFormat, FormatWidth} from '../../src/i18n/locale_data_api'; { describe('locale data api', () => { @@ -22,6 +23,7 @@ import {findLocaleData, getCurrencySymbol} from '../../src/i18n/locale_data_api' registerLocaleData(localeFrCA); registerLocaleData(localeFr, 'fake-id'); registerLocaleData(localeFrCA, 'fake_Id2'); + registerLocaleData(localeZh); }); describe('findLocaleData', () => { @@ -64,5 +66,10 @@ import {findLocaleData, getCurrencySymbol} from '../../src/i18n/locale_data_api' expect(getCurrencySymbol('FAKE', 'narrow')).toEqual('FAKE'); }); }); + + describe('getLastDefinedValue', () => { + it('should find the last defined date format when format not defined', + () => { expect(getLocaleDateFormat('zh', FormatWidth.Long)).toEqual('y年M月d日'); }); + }); }); }