fix(common): fallback to last defined value for named date and time formats (#21299)

closes #21282

PR Close #21299
This commit is contained in:
Trotyl 2018-01-04 15:35:57 +08:00 committed by Miško Hevery
parent 2cf9d6d75b
commit 879756d44c
2 changed files with 10 additions and 3 deletions

View File

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

View File

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