diff --git a/src/lib/locale/locales.js b/src/lib/locale/locales.js index 7d8fb26e03..484cb28994 100644 --- a/src/lib/locale/locales.js +++ b/src/lib/locale/locales.js @@ -42,7 +42,7 @@ function chooseLocale(names) { } i++; } - return null; + return globalLocale; } function loadLocale(name) { @@ -84,7 +84,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', @@ -97,14 +97,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)); diff --git a/src/test/moment/locale_inheritance.js b/src/test/moment/locale_inheritance.js index b5e718db9f..133bb58b0d 100644 --- a/src/test/moment/locale_inheritance.js +++ b/src/test/moment/locale_inheritance.js @@ -173,6 +173,9 @@ 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('_') }); @@ -180,3 +183,13 @@ test('define child locale before parent', function (assert) { 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'); +});