Skip to content

Commit 1b46e8a

Browse files
committed
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 select2#2934.
1 parent 2f4cc19 commit 1b46e8a

7 files changed

Lines changed: 138 additions & 23 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3662,7 +3662,16 @@ define('select2/defaults',[
36623662
}
36633663

36643664
if (typeof options.language === 'string') {
3665-
options.language = [options.language];
3665+
// Check if the lanugage is specified with a region
3666+
if (options.language.indexOf('-') > 0) {
3667+
// Extract the region information if it is included
3668+
var languageParts = options.language.split('-');
3669+
var baseLanguage = languageParts[0];
3670+
3671+
options.language = [options.language, baseLanguage];
3672+
} else {
3673+
options.language = [options.language];
3674+
}
36663675
}
36673676

36683677
if ($.isArray(options.language)) {
@@ -3679,9 +3688,23 @@ define('select2/defaults',[
36793688
// Try to load it with the original name
36803689
language = Translation.loadPath(name);
36813690
} catch (e) {
3682-
// If we couldn't load it, check if it wasn't the full path
3683-
name = this.defaults.amdLanguageBase + name;
3684-
language = Translation.loadPath(name);
3691+
try {
3692+
// If we couldn't load it, check if it wasn't the full path
3693+
name = this.defaults.amdLanguageBase + name;
3694+
language = Translation.loadPath(name);
3695+
} catch (ex) {
3696+
// The translation could not be loaded at all. Sometimes this is
3697+
// because of a configuration problem, other times this can be
3698+
// because of how Select2 helps load all possible translation files.
3699+
if (console && console.warn) {
3700+
console.warn(
3701+
'Select2: The lanugage file for "' + name + '" could not be ' +
3702+
'automatically loaded. A fallback will be used instead.'
3703+
);
3704+
}
3705+
3706+
continue;
3707+
}
36853708
}
36863709

36873710
languages.extend(language);

dist/js/select2.amd.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3662,7 +3662,16 @@ define('select2/defaults',[
36623662
}
36633663

36643664
if (typeof options.language === 'string') {
3665-
options.language = [options.language];
3665+
// Check if the lanugage is specified with a region
3666+
if (options.language.indexOf('-') > 0) {
3667+
// Extract the region information if it is included
3668+
var languageParts = options.language.split('-');
3669+
var baseLanguage = languageParts[0];
3670+
3671+
options.language = [options.language, baseLanguage];
3672+
} else {
3673+
options.language = [options.language];
3674+
}
36663675
}
36673676

36683677
if ($.isArray(options.language)) {
@@ -3679,9 +3688,23 @@ define('select2/defaults',[
36793688
// Try to load it with the original name
36803689
language = Translation.loadPath(name);
36813690
} catch (e) {
3682-
// If we couldn't load it, check if it wasn't the full path
3683-
name = this.defaults.amdLanguageBase + name;
3684-
language = Translation.loadPath(name);
3691+
try {
3692+
// If we couldn't load it, check if it wasn't the full path
3693+
name = this.defaults.amdLanguageBase + name;
3694+
language = Translation.loadPath(name);
3695+
} catch (ex) {
3696+
// The translation could not be loaded at all. Sometimes this is
3697+
// because of a configuration problem, other times this can be
3698+
// because of how Select2 helps load all possible translation files.
3699+
if (console && console.warn) {
3700+
console.warn(
3701+
'Select2: The lanugage file for "' + name + '" could not be ' +
3702+
'automatically loaded. A fallback will be used instead.'
3703+
);
3704+
}
3705+
3706+
continue;
3707+
}
36853708
}
36863709

36873710
languages.extend(language);

dist/js/select2.full.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13197,7 +13197,16 @@ define('select2/defaults',[
1319713197
}
1319813198

1319913199
if (typeof options.language === 'string') {
13200-
options.language = [options.language];
13200+
// Check if the lanugage is specified with a region
13201+
if (options.language.indexOf('-') > 0) {
13202+
// Extract the region information if it is included
13203+
var languageParts = options.language.split('-');
13204+
var baseLanguage = languageParts[0];
13205+
13206+
options.language = [options.language, baseLanguage];
13207+
} else {
13208+
options.language = [options.language];
13209+
}
1320113210
}
1320213211

1320313212
if ($.isArray(options.language)) {
@@ -13214,9 +13223,23 @@ define('select2/defaults',[
1321413223
// Try to load it with the original name
1321513224
language = Translation.loadPath(name);
1321613225
} catch (e) {
13217-
// If we couldn't load it, check if it wasn't the full path
13218-
name = this.defaults.amdLanguageBase + name;
13219-
language = Translation.loadPath(name);
13226+
try {
13227+
// If we couldn't load it, check if it wasn't the full path
13228+
name = this.defaults.amdLanguageBase + name;
13229+
language = Translation.loadPath(name);
13230+
} catch (ex) {
13231+
// The translation could not be loaded at all. Sometimes this is
13232+
// because of a configuration problem, other times this can be
13233+
// because of how Select2 helps load all possible translation files.
13234+
if (console && console.warn) {
13235+
console.warn(
13236+
'Select2: The lanugage file for "' + name + '" could not be ' +
13237+
'automatically loaded. A fallback will be used instead.'
13238+
);
13239+
}
13240+
13241+
continue;
13242+
}
1322013243
}
1322113244

1322213245
languages.extend(language);

dist/js/select2.full.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4090,7 +4090,16 @@ define('select2/defaults',[
40904090
}
40914091

40924092
if (typeof options.language === 'string') {
4093-
options.language = [options.language];
4093+
// Check if the lanugage is specified with a region
4094+
if (options.language.indexOf('-') > 0) {
4095+
// Extract the region information if it is included
4096+
var languageParts = options.language.split('-');
4097+
var baseLanguage = languageParts[0];
4098+
4099+
options.language = [options.language, baseLanguage];
4100+
} else {
4101+
options.language = [options.language];
4102+
}
40944103
}
40954104

40964105
if ($.isArray(options.language)) {
@@ -4107,9 +4116,23 @@ define('select2/defaults',[
41074116
// Try to load it with the original name
41084117
language = Translation.loadPath(name);
41094118
} catch (e) {
4110-
// If we couldn't load it, check if it wasn't the full path
4111-
name = this.defaults.amdLanguageBase + name;
4112-
language = Translation.loadPath(name);
4119+
try {
4120+
// If we couldn't load it, check if it wasn't the full path
4121+
name = this.defaults.amdLanguageBase + name;
4122+
language = Translation.loadPath(name);
4123+
} catch (ex) {
4124+
// The translation could not be loaded at all. Sometimes this is
4125+
// because of a configuration problem, other times this can be
4126+
// because of how Select2 helps load all possible translation files.
4127+
if (console && console.warn) {
4128+
console.warn(
4129+
'Select2: The lanugage file for "' + name + '" could not be ' +
4130+
'automatically loaded. A fallback will be used instead.'
4131+
);
4132+
}
4133+
4134+
continue;
4135+
}
41134136
}
41144137

41154138
languages.extend(language);

dist/js/select2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/select2/defaults.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,16 @@ define([
195195
}
196196

197197
if (typeof options.language === 'string') {
198-
options.language = [options.language];
198+
// Check if the lanugage is specified with a region
199+
if (options.language.indexOf('-') > 0) {
200+
// Extract the region information if it is included
201+
var languageParts = options.language.split('-');
202+
var baseLanguage = languageParts[0];
203+
204+
options.language = [options.language, baseLanguage];
205+
} else {
206+
options.language = [options.language];
207+
}
199208
}
200209

201210
if ($.isArray(options.language)) {
@@ -212,9 +221,23 @@ define([
212221
// Try to load it with the original name
213222
language = Translation.loadPath(name);
214223
} catch (e) {
215-
// If we couldn't load it, check if it wasn't the full path
216-
name = this.defaults.amdLanguageBase + name;
217-
language = Translation.loadPath(name);
224+
try {
225+
// If we couldn't load it, check if it wasn't the full path
226+
name = this.defaults.amdLanguageBase + name;
227+
language = Translation.loadPath(name);
228+
} catch (ex) {
229+
// The translation could not be loaded at all. Sometimes this is
230+
// because of a configuration problem, other times this can be
231+
// because of how Select2 helps load all possible translation files.
232+
if (console && console.warn) {
233+
console.warn(
234+
'Select2: The lanugage file for "' + name + '" could not be ' +
235+
'automatically loaded. A fallback will be used instead.'
236+
);
237+
}
238+
239+
continue;
240+
}
218241
}
219242

220243
languages.extend(language);

0 commit comments

Comments
 (0)