Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 0be1da1

Browse files
committed
Fallback on locale name from specific to generic
1 parent 881b702 commit 0be1da1

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

src/code/data-loader.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,27 @@ function loadLocaleData(packedCLDRZoneData) {
346346
});
347347
}
348348

349+
function getLocale(locale) {
350+
if (this.get(locale)) {
351+
return this.get(locale);
352+
}
353+
354+
const localeItems = locale.split('-');
355+
const fallbackLocales = [];
356+
357+
for (let i = 0; i < localeItems.length - 1; i++) {
358+
fallbackLocales.push(localeItems.slice(0, localeItems.length - i - 1).join('-'));
359+
}
360+
361+
for (let j = 0; j < fallbackLocales.length; j++) {
362+
if (this.get(fallbackLocales[j])) {
363+
return this.get(fallbackLocales[j]);
364+
}
365+
}
366+
367+
return undefined;
368+
}
369+
349370
/**
350371
* Class every loader is instance.
351372
* data files call load function on this object.
@@ -384,7 +405,9 @@ export default function dataloader(globalSpace) {
384405
globalSpace.Intl.<someNameSpace>.value : will hold the value.
385406
*/
386407
globalSpace.Intl._metaZoneData = new Loader();
387-
globalSpace.Intl._localeData = new Loader(loadLocaleData);
408+
globalSpace.Intl._localeData = new Loader(loadLocaleData, {
409+
getLocale: getLocale
410+
});
388411
globalSpace.Intl._timeZoneData = new Loader(loadTimeZoneData, {
389412
// this timeZone loader needs to modify _metaZoneData so it need access to global object.
390413
// this is why this function is bind to globalSpace

src/code/lookup-utill.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function getTimeZoneOffsetInfo(timeZoneHistory, date) {
126126
*/
127127
function getZoneNameForLocale({locale, ianaTimeZone, offset, isdst, isShort, timeStamp}) {
128128
const metaZoneName = getRelevantMetaZone(Intl._metaZoneData.get(ianaTimeZone), timeStamp, getGenericZoneName(offset));
129-
const cldrZones = Intl._localeData.get(locale);
129+
const cldrZones = Intl._localeData.getLocale(locale);
130130
const cldrZoneNamesThruMetaZone = (metaZoneName && cldrZones && cldrZones.metazone[metaZoneName]);
131131
const cldrZoneNamesThruIanaTimeZone = (cldrZones && cldrZones.zone && cldrZones.zone[ianaTimeZone]);
132132

src/code/polyfill.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export default function polyfill(globalSpace) {
124124
if (options.timeZoneName !== undefined) {
125125
// We need to include timeZoneName in date format.
126126
// Check if we have locale data to able to do that.
127-
if (!(gIntl._localeData.get(resolvedLocale) && // availability of localedata
127+
if (!(gIntl._localeData.getLocale(resolvedLocale) && // availability of localedata
128128
Intl._metaZoneData.get(timeZone))) { // availability of metaZone for this timeZone
129129
throw new RangeError(`unsupported value "${options.timeZoneName}" for timeZone ${timeZone}. requires locale data for ${resolvedLocale}`);
130130
}

test/test-complete.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,26 @@ describe('Polyfill with complete package', () => {
276276
});
277277
});
278278
});
279+
280+
describe('globalSpace.Intl._localeData', () => {
281+
const ld = Intl._localeData;
282+
283+
describe('getLocale', () => {
284+
it('finds valid locale', () => {
285+
assert(ld.getLocale('en-US-u-va-posix'));
286+
assert(ld.getLocale('en'));
287+
});
288+
289+
it('should not find an invalid locale', () => {
290+
assert(!ld.getLocale('noooo-not-a-locale'));
291+
});
292+
293+
it('fallsback on generic locale if specific locale is not found', () => {
294+
const loadedLocales = Object.keys(ld.get());
295+
296+
loadedLocales.forEach(locale => assert(ld.getLocale(locale + '-abcd')));
297+
loadedLocales.forEach(locale => assert(ld.getLocale(locale + '-abcd-bcb')));
298+
});
299+
});
300+
});
279301
});

0 commit comments

Comments
 (0)