1
0
mirror of synced 2024-11-26 14:56:07 +03:00

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:
Kevin Brown 2015-01-17 21:56:58 -05:00
parent 2f4cc19cd9
commit 1b46e8ab3e
7 changed files with 138 additions and 23 deletions

View File

@ -3662,8 +3662,17 @@ define('select2/defaults',[
} }
if (typeof options.language === 'string') { 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]; options.language = [options.language];
} }
}
if ($.isArray(options.language)) { if ($.isArray(options.language)) {
var languages = new Translation(); var languages = new Translation();
@ -3679,9 +3688,23 @@ define('select2/defaults',[
// Try to load it with the original name // Try to load it with the original name
language = Translation.loadPath(name); language = Translation.loadPath(name);
} catch (e) { } catch (e) {
try {
// If we couldn't load it, check if it wasn't the full path // If we couldn't load it, check if it wasn't the full path
name = this.defaults.amdLanguageBase + name; name = this.defaults.amdLanguageBase + name;
language = Translation.loadPath(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); languages.extend(language);

View File

@ -3662,8 +3662,17 @@ define('select2/defaults',[
} }
if (typeof options.language === 'string') { 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]; options.language = [options.language];
} }
}
if ($.isArray(options.language)) { if ($.isArray(options.language)) {
var languages = new Translation(); var languages = new Translation();
@ -3679,9 +3688,23 @@ define('select2/defaults',[
// Try to load it with the original name // Try to load it with the original name
language = Translation.loadPath(name); language = Translation.loadPath(name);
} catch (e) { } catch (e) {
try {
// If we couldn't load it, check if it wasn't the full path // If we couldn't load it, check if it wasn't the full path
name = this.defaults.amdLanguageBase + name; name = this.defaults.amdLanguageBase + name;
language = Translation.loadPath(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); languages.extend(language);

View File

@ -13197,8 +13197,17 @@ define('select2/defaults',[
} }
if (typeof options.language === 'string') { 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]; options.language = [options.language];
} }
}
if ($.isArray(options.language)) { if ($.isArray(options.language)) {
var languages = new Translation(); var languages = new Translation();
@ -13214,9 +13223,23 @@ define('select2/defaults',[
// Try to load it with the original name // Try to load it with the original name
language = Translation.loadPath(name); language = Translation.loadPath(name);
} catch (e) { } catch (e) {
try {
// If we couldn't load it, check if it wasn't the full path // If we couldn't load it, check if it wasn't the full path
name = this.defaults.amdLanguageBase + name; name = this.defaults.amdLanguageBase + name;
language = Translation.loadPath(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); languages.extend(language);

File diff suppressed because one or more lines are too long

23
dist/js/select2.js vendored
View File

@ -4090,8 +4090,17 @@ define('select2/defaults',[
} }
if (typeof options.language === 'string') { 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]; options.language = [options.language];
} }
}
if ($.isArray(options.language)) { if ($.isArray(options.language)) {
var languages = new Translation(); var languages = new Translation();
@ -4107,9 +4116,23 @@ define('select2/defaults',[
// Try to load it with the original name // Try to load it with the original name
language = Translation.loadPath(name); language = Translation.loadPath(name);
} catch (e) { } catch (e) {
try {
// If we couldn't load it, check if it wasn't the full path // If we couldn't load it, check if it wasn't the full path
name = this.defaults.amdLanguageBase + name; name = this.defaults.amdLanguageBase + name;
language = Translation.loadPath(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); languages.extend(language);

File diff suppressed because one or more lines are too long

View File

@ -195,8 +195,17 @@ define([
} }
if (typeof options.language === 'string') { 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]; options.language = [options.language];
} }
}
if ($.isArray(options.language)) { if ($.isArray(options.language)) {
var languages = new Translation(); var languages = new Translation();
@ -212,9 +221,23 @@ define([
// Try to load it with the original name // Try to load it with the original name
language = Translation.loadPath(name); language = Translation.loadPath(name);
} catch (e) { } catch (e) {
try {
// If we couldn't load it, check if it wasn't the full path // If we couldn't load it, check if it wasn't the full path
name = this.defaults.amdLanguageBase + name; name = this.defaults.amdLanguageBase + name;
language = Translation.loadPath(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); languages.extend(language);