Skip to content

Commit

Permalink
fix(common): fallback to last defined value for named date and time f…
Browse files Browse the repository at this point in the history
…ormats (#21299)

closes #21282

PR Close #21299
  • Loading branch information
trotyl authored and mhevery committed Jan 19, 2018
1 parent 3606c55 commit 982eb7b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/common/src/i18n/locale_data_api.ts
Expand Up @@ -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);
}

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

/**
Expand Down
9 changes: 8 additions & 1 deletion packages/common/test/i18n/locale_data_api_spec.ts
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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日'); });
});
});
}

0 comments on commit 982eb7b

Please sign in to comment.