Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] Fix #3883 lazy load parentLocale in defineLocale, fallback to global if missing #4310

Merged
merged 3 commits into from Mar 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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',
Expand All @@ -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));
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');
});