Improve the default language chain
If the language that is passed in through the options is just a string, like "en-US" or "pt-BR", Select2 will now try to load the base language along with the requested language and "en". So requesting "pt-BR" will try to load "pt-BR", "pt", and "en". This also catches errors and triggers a warning about not being able to load the file. This should be a more user friendly error, and it fixes the problem where Select2 would completely fail to initialize if a language could not be found. This closes https://github.com/select2/select2/issues/2934.
This commit is contained in:
parent
2f4cc19cd9
commit
1b46e8ab3e
23
dist/js/select2.amd.full.js
vendored
23
dist/js/select2.amd.full.js
vendored
@ -3662,8 +3662,17 @@ define('select2/defaults',[
|
||||
}
|
||||
|
||||
if (typeof options.language === 'string') {
|
||||
// Check if the lanugage is specified with a region
|
||||
if (options.language.indexOf('-') > 0) {
|
||||
// Extract the region information if it is included
|
||||
var languageParts = options.language.split('-');
|
||||
var baseLanguage = languageParts[0];
|
||||
|
||||
options.language = [options.language, baseLanguage];
|
||||
} else {
|
||||
options.language = [options.language];
|
||||
}
|
||||
}
|
||||
|
||||
if ($.isArray(options.language)) {
|
||||
var languages = new Translation();
|
||||
@ -3679,9 +3688,23 @@ define('select2/defaults',[
|
||||
// Try to load it with the original name
|
||||
language = Translation.loadPath(name);
|
||||
} catch (e) {
|
||||
try {
|
||||
// If we couldn't load it, check if it wasn't the full path
|
||||
name = this.defaults.amdLanguageBase + name;
|
||||
language = Translation.loadPath(name);
|
||||
} catch (ex) {
|
||||
// The translation could not be loaded at all. Sometimes this is
|
||||
// because of a configuration problem, other times this can be
|
||||
// because of how Select2 helps load all possible translation files.
|
||||
if (console && console.warn) {
|
||||
console.warn(
|
||||
'Select2: The lanugage file for "' + name + '" could not be ' +
|
||||
'automatically loaded. A fallback will be used instead.'
|
||||
);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
languages.extend(language);
|
||||
|
23
dist/js/select2.amd.js
vendored
23
dist/js/select2.amd.js
vendored
@ -3662,8 +3662,17 @@ define('select2/defaults',[
|
||||
}
|
||||
|
||||
if (typeof options.language === 'string') {
|
||||
// Check if the lanugage is specified with a region
|
||||
if (options.language.indexOf('-') > 0) {
|
||||
// Extract the region information if it is included
|
||||
var languageParts = options.language.split('-');
|
||||
var baseLanguage = languageParts[0];
|
||||
|
||||
options.language = [options.language, baseLanguage];
|
||||
} else {
|
||||
options.language = [options.language];
|
||||
}
|
||||
}
|
||||
|
||||
if ($.isArray(options.language)) {
|
||||
var languages = new Translation();
|
||||
@ -3679,9 +3688,23 @@ define('select2/defaults',[
|
||||
// Try to load it with the original name
|
||||
language = Translation.loadPath(name);
|
||||
} catch (e) {
|
||||
try {
|
||||
// If we couldn't load it, check if it wasn't the full path
|
||||
name = this.defaults.amdLanguageBase + name;
|
||||
language = Translation.loadPath(name);
|
||||
} catch (ex) {
|
||||
// The translation could not be loaded at all. Sometimes this is
|
||||
// because of a configuration problem, other times this can be
|
||||
// because of how Select2 helps load all possible translation files.
|
||||
if (console && console.warn) {
|
||||
console.warn(
|
||||
'Select2: The lanugage file for "' + name + '" could not be ' +
|
||||
'automatically loaded. A fallback will be used instead.'
|
||||
);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
languages.extend(language);
|
||||
|
23
dist/js/select2.full.js
vendored
23
dist/js/select2.full.js
vendored
@ -13197,8 +13197,17 @@ define('select2/defaults',[
|
||||
}
|
||||
|
||||
if (typeof options.language === 'string') {
|
||||
// Check if the lanugage is specified with a region
|
||||
if (options.language.indexOf('-') > 0) {
|
||||
// Extract the region information if it is included
|
||||
var languageParts = options.language.split('-');
|
||||
var baseLanguage = languageParts[0];
|
||||
|
||||
options.language = [options.language, baseLanguage];
|
||||
} else {
|
||||
options.language = [options.language];
|
||||
}
|
||||
}
|
||||
|
||||
if ($.isArray(options.language)) {
|
||||
var languages = new Translation();
|
||||
@ -13214,9 +13223,23 @@ define('select2/defaults',[
|
||||
// Try to load it with the original name
|
||||
language = Translation.loadPath(name);
|
||||
} catch (e) {
|
||||
try {
|
||||
// If we couldn't load it, check if it wasn't the full path
|
||||
name = this.defaults.amdLanguageBase + name;
|
||||
language = Translation.loadPath(name);
|
||||
} catch (ex) {
|
||||
// The translation could not be loaded at all. Sometimes this is
|
||||
// because of a configuration problem, other times this can be
|
||||
// because of how Select2 helps load all possible translation files.
|
||||
if (console && console.warn) {
|
||||
console.warn(
|
||||
'Select2: The lanugage file for "' + name + '" could not be ' +
|
||||
'automatically loaded. A fallback will be used instead.'
|
||||
);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
languages.extend(language);
|
||||
|
4
dist/js/select2.full.min.js
vendored
4
dist/js/select2.full.min.js
vendored
File diff suppressed because one or more lines are too long
23
dist/js/select2.js
vendored
23
dist/js/select2.js
vendored
@ -4090,8 +4090,17 @@ define('select2/defaults',[
|
||||
}
|
||||
|
||||
if (typeof options.language === 'string') {
|
||||
// Check if the lanugage is specified with a region
|
||||
if (options.language.indexOf('-') > 0) {
|
||||
// Extract the region information if it is included
|
||||
var languageParts = options.language.split('-');
|
||||
var baseLanguage = languageParts[0];
|
||||
|
||||
options.language = [options.language, baseLanguage];
|
||||
} else {
|
||||
options.language = [options.language];
|
||||
}
|
||||
}
|
||||
|
||||
if ($.isArray(options.language)) {
|
||||
var languages = new Translation();
|
||||
@ -4107,9 +4116,23 @@ define('select2/defaults',[
|
||||
// Try to load it with the original name
|
||||
language = Translation.loadPath(name);
|
||||
} catch (e) {
|
||||
try {
|
||||
// If we couldn't load it, check if it wasn't the full path
|
||||
name = this.defaults.amdLanguageBase + name;
|
||||
language = Translation.loadPath(name);
|
||||
} catch (ex) {
|
||||
// The translation could not be loaded at all. Sometimes this is
|
||||
// because of a configuration problem, other times this can be
|
||||
// because of how Select2 helps load all possible translation files.
|
||||
if (console && console.warn) {
|
||||
console.warn(
|
||||
'Select2: The lanugage file for "' + name + '" could not be ' +
|
||||
'automatically loaded. A fallback will be used instead.'
|
||||
);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
languages.extend(language);
|
||||
|
2
dist/js/select2.min.js
vendored
2
dist/js/select2.min.js
vendored
File diff suppressed because one or more lines are too long
23
src/js/select2/defaults.js
vendored
23
src/js/select2/defaults.js
vendored
@ -195,8 +195,17 @@ define([
|
||||
}
|
||||
|
||||
if (typeof options.language === 'string') {
|
||||
// Check if the lanugage is specified with a region
|
||||
if (options.language.indexOf('-') > 0) {
|
||||
// Extract the region information if it is included
|
||||
var languageParts = options.language.split('-');
|
||||
var baseLanguage = languageParts[0];
|
||||
|
||||
options.language = [options.language, baseLanguage];
|
||||
} else {
|
||||
options.language = [options.language];
|
||||
}
|
||||
}
|
||||
|
||||
if ($.isArray(options.language)) {
|
||||
var languages = new Translation();
|
||||
@ -212,9 +221,23 @@ define([
|
||||
// Try to load it with the original name
|
||||
language = Translation.loadPath(name);
|
||||
} catch (e) {
|
||||
try {
|
||||
// If we couldn't load it, check if it wasn't the full path
|
||||
name = this.defaults.amdLanguageBase + name;
|
||||
language = Translation.loadPath(name);
|
||||
} catch (ex) {
|
||||
// The translation could not be loaded at all. Sometimes this is
|
||||
// because of a configuration problem, other times this can be
|
||||
// because of how Select2 helps load all possible translation files.
|
||||
if (console && console.warn) {
|
||||
console.warn(
|
||||
'Select2: The lanugage file for "' + name + '" could not be ' +
|
||||
'automatically loaded. A fallback will be used instead.'
|
||||
);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
languages.extend(language);
|
||||
|
Loading…
Reference in New Issue
Block a user