]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[bugfix] Fix #3883 lazy load parentLocale in defineLocale, fallback to global if...
authorChris Myers <cmyers2015@outlook.com>
Fri, 2 Mar 2018 18:59:25 +0000 (18:59 +0000)
committerKunal Marwaha <marwahaha@berkeley.edu>
Fri, 2 Mar 2018 18:59:25 +0000 (10:59 -0800)
* 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

src/lib/locale/locales.js
src/test/moment/locale_inheritance.js

index 131d87e4ca12bc2b6463e4f043f46c5d54646c92..af28bfe27f74ab1ed94d1cd6296e43b85a16f8a5 100644 (file)
@@ -42,7 +42,7 @@ function chooseLocale(names) {
         }
         i++;
     }
-    return null;
+    return globalLocale;
 }
 
 function loadLocale(name) {
@@ -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',
@@ -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));
index b5e718db9f7297a6236143db08cc89cb436610e1..133bb58b0d6b7a0916fc488210874688a9c37f8a 100644 (file)
@@ -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');
+});