Skip to content

Commit

Permalink
[bugfix] Fix #3883 lazy load parentLocale in defineLocale, fallback t…
Browse files Browse the repository at this point in the history
…o global if missing (#4310)

* Fix #3883 defineLocale lazy load parentLocale

* Fix #3883 fallback to globalLocale if parentLocale can't be loaded,
added test

* Fix #3883 lazy load parentLocale test
  • Loading branch information
cmyers authored and marwahaha committed Mar 2, 2018
1 parent bb142fc commit f6c7069
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/lib/locale/locales.js
Expand Up @@ -42,7 +42,7 @@ function chooseLocale(names) {
}
i++;
}
return null;
return globalLocale;
}

function loadLocale(name) {
Expand Down Expand Up @@ -90,7 +90,7 @@ export function getSetGlobalLocale (key, values) {

export function defineLocale (name, config) {
if (config !== null) {
var parentConfig = baseConfig;
var locale, parentConfig = baseConfig;
config.abbr = name;
if (locales[name] != null) {
deprecateSimple('defineLocaleOverride',
Expand All @@ -103,14 +103,19 @@ export function defineLocale (name, config) {
if (locales[config.parentLocale] != null) {
parentConfig = locales[config.parentLocale]._config;
} else {
if (!localeFamilies[config.parentLocale]) {
localeFamilies[config.parentLocale] = [];
locale = loadLocale(config.parentLocale);
if (locale != null) {
parentConfig = locale._config;
} else {
if (!localeFamilies[config.parentLocale]) {
localeFamilies[config.parentLocale] = [];
}
localeFamilies[config.parentLocale].push({
name: name,
config: config
});
return null;
}
localeFamilies[config.parentLocale].push({
name: name,
config: config
});
return null;
}
}
locales[name] = new Locale(mergeConfigs(parentConfig, config));
Expand Down
13 changes: 13 additions & 0 deletions src/test/moment/locale_inheritance.js
Expand Up @@ -173,10 +173,23 @@ test('define child locale before parent', function (assert) {
months : 'First_Second_Third_Fourth_Fifth_Sixth_Seventh_Eighth_Ninth_Tenth_Eleventh_Twelveth '.split('_')
});
assert.equal(moment.locale(), 'en', 'failed to set a locale requiring missing parent');

assert.equal(moment('00:00:00 01/January/2017', 'HH:mm:ss DD/MMM/YYYY', 'months-x').locale(), 'en', 'creating moment using child with undefined parent defaults to global');

moment.defineLocale('base-months-x', {
months : 'One_Two_Three_Four_Five_Six_Seven_Eight_Nine_Ten_Eleven_Twelve'.split('_')
});
assert.equal(moment.locale(), 'base-months-x', 'defineLocale should also set the locale (regardless of child locales)');

assert.equal(moment().locale('months-x').month(0).format('MMMM'), 'First', 'loading child before parent locale works');
});

test('lazy load parentLocale', function (assert) {
moment.defineLocale('de', null);

moment.defineLocale('de_test', {
parentLocale: 'de',
monthsShort: ['M1', 'M2', 'M3', 'M4', 'M5', 'M6', 'M7', 'M8', 'M9', 'M10', 'M11', 'M12']
});
assert.equal(moment.locale(), 'de_test', 'failed to lazy load parentLocale');
});

0 comments on commit f6c7069

Please sign in to comment.