Skip to content

Commit 9bb993c

Browse files
committed
Added Translation object
This continues the work to get translations built into Select2.
1 parent 20fcaa4 commit 9bb993c

11 files changed

Lines changed: 467 additions & 41 deletions

File tree

dist/js/i18n/en.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.

dist/js/select2.amd.full.js

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }define('select2/utils',[], function () {
1+
window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }define('select2/utils',[], function () {
22
var Utils = {};
33

44
Utils.Extend = function (ChildClass, SuperClass) {
@@ -837,6 +837,42 @@ define('select2/selection/placeholder',[
837837
return Placeholder;
838838
});
839839

840+
define('select2/translation',[
841+
842+
], function () {
843+
function Translation (dict) {
844+
this.dict = dict || {};
845+
}
846+
847+
Translation.prototype.all = function () {
848+
return this.dict;
849+
};
850+
851+
Translation.prototype.get = function (key) {
852+
return this.dict[key];
853+
};
854+
855+
Translation.prototype.extend = function (translation) {
856+
this.dict = $.extend({}, translation.all(), this.dict);
857+
};
858+
859+
// Static functions
860+
861+
Translation._cache = {};
862+
863+
Translation.loadPath = function (path) {
864+
if (!(path in Translation._cache)) {
865+
var translations = require(path);
866+
867+
Translation._cache[path] = translations;
868+
}
869+
870+
return new Translation(Translation._cache[path]);
871+
};
872+
873+
return Translation;
874+
});
875+
840876
define('select2/data/base',[
841877
'../utils'
842878
], function (Utils) {
@@ -1345,33 +1381,45 @@ define('select2/dropdown/search',[
13451381
return Search;
13461382
});
13471383

1384+
define('select2/i18n/en',[],function () {
1385+
return {
1386+
'no_results': function () {
1387+
return 'No results found';
1388+
}
1389+
};
1390+
});
1391+
13481392
define('select2/defaults',[
1393+
'jquery',
13491394
'./results',
13501395

13511396
'./selection/single',
13521397
'./selection/multiple',
13531398
'./selection/placeholder',
13541399

13551400
'./utils',
1401+
'./translation',
13561402

13571403
'./data/select',
13581404
'./data/array',
13591405
'./data/ajax',
13601406
'./data/tags',
13611407

13621408
'./dropdown',
1363-
'./dropdown/search'
1364-
], function (ResultsList,
1409+
'./dropdown/search',
1410+
1411+
'./i18n/en'
1412+
], function ($, ResultsList,
13651413
SingleSelection, MultipleSelection, Placeholder,
1366-
Utils,
1414+
Utils, Translation,
13671415
SelectData, ArrayData, AjaxData, Tags,
1368-
Dropdown, Search) {
1416+
Dropdown, Search, EnglishTranslation) {
13691417
function Defaults () {
13701418
this.reset();
13711419
}
13721420

13731421
Defaults.prototype.apply = function (options) {
1374-
options = $.extend({}, options, this.defaults);
1422+
options = $.extend({}, this.defaults, options);
13751423

13761424
if (options.dataAdapter == null) {
13771425
if (options.ajax) {
@@ -1413,11 +1461,42 @@ define('select2/defaults',[
14131461
}
14141462
}
14151463

1464+
if (typeof options.language === 'string') {
1465+
options.language = [options.language];
1466+
}
1467+
1468+
if ($.isArray(options.language)) {
1469+
var languages = new Translation();
1470+
var languageNames = options.language.concat(this.defaults.language);
1471+
1472+
for (var l = 0; l < languageNames.length; l++) {
1473+
var name = languageNames[l];
1474+
var language = {};
1475+
1476+
try {
1477+
// Try to load it with the original name
1478+
language = Translation.loadPath(name);
1479+
} catch (e) {
1480+
// If we couldn't load it, check if it wasn't the full path
1481+
name = 'select2/i18n/' + name;
1482+
language = Translation.loadPath(name);
1483+
}
1484+
1485+
languages.extend(language);
1486+
}
1487+
1488+
options.translations = languages;
1489+
} else {
1490+
options.translations = new Translations(options.language);
1491+
}
1492+
14161493
return options;
14171494
};
14181495

14191496
Defaults.prototype.reset = function () {
1420-
this.defaults = { };
1497+
this.defaults = {
1498+
language: ['select2/i18n/en']
1499+
};
14211500
};
14221501

14231502
var defaults = new Defaults();

dist/js/select2.amd.js

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }define('select2/utils',[], function () {
1+
window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }define('select2/utils',[], function () {
22
var Utils = {};
33

44
Utils.Extend = function (ChildClass, SuperClass) {
@@ -837,6 +837,42 @@ define('select2/selection/placeholder',[
837837
return Placeholder;
838838
});
839839

840+
define('select2/translation',[
841+
842+
], function () {
843+
function Translation (dict) {
844+
this.dict = dict || {};
845+
}
846+
847+
Translation.prototype.all = function () {
848+
return this.dict;
849+
};
850+
851+
Translation.prototype.get = function (key) {
852+
return this.dict[key];
853+
};
854+
855+
Translation.prototype.extend = function (translation) {
856+
this.dict = $.extend({}, translation.all(), this.dict);
857+
};
858+
859+
// Static functions
860+
861+
Translation._cache = {};
862+
863+
Translation.loadPath = function (path) {
864+
if (!(path in Translation._cache)) {
865+
var translations = require(path);
866+
867+
Translation._cache[path] = translations;
868+
}
869+
870+
return new Translation(Translation._cache[path]);
871+
};
872+
873+
return Translation;
874+
});
875+
840876
define('select2/data/base',[
841877
'../utils'
842878
], function (Utils) {
@@ -1345,33 +1381,45 @@ define('select2/dropdown/search',[
13451381
return Search;
13461382
});
13471383

1384+
define('select2/i18n/en',[],function () {
1385+
return {
1386+
'no_results': function () {
1387+
return 'No results found';
1388+
}
1389+
};
1390+
});
1391+
13481392
define('select2/defaults',[
1393+
'jquery',
13491394
'./results',
13501395

13511396
'./selection/single',
13521397
'./selection/multiple',
13531398
'./selection/placeholder',
13541399

13551400
'./utils',
1401+
'./translation',
13561402

13571403
'./data/select',
13581404
'./data/array',
13591405
'./data/ajax',
13601406
'./data/tags',
13611407

13621408
'./dropdown',
1363-
'./dropdown/search'
1364-
], function (ResultsList,
1409+
'./dropdown/search',
1410+
1411+
'./i18n/en'
1412+
], function ($, ResultsList,
13651413
SingleSelection, MultipleSelection, Placeholder,
1366-
Utils,
1414+
Utils, Translation,
13671415
SelectData, ArrayData, AjaxData, Tags,
1368-
Dropdown, Search) {
1416+
Dropdown, Search, EnglishTranslation) {
13691417
function Defaults () {
13701418
this.reset();
13711419
}
13721420

13731421
Defaults.prototype.apply = function (options) {
1374-
options = $.extend({}, options, this.defaults);
1422+
options = $.extend({}, this.defaults, options);
13751423

13761424
if (options.dataAdapter == null) {
13771425
if (options.ajax) {
@@ -1413,11 +1461,42 @@ define('select2/defaults',[
14131461
}
14141462
}
14151463

1464+
if (typeof options.language === 'string') {
1465+
options.language = [options.language];
1466+
}
1467+
1468+
if ($.isArray(options.language)) {
1469+
var languages = new Translation();
1470+
var languageNames = options.language.concat(this.defaults.language);
1471+
1472+
for (var l = 0; l < languageNames.length; l++) {
1473+
var name = languageNames[l];
1474+
var language = {};
1475+
1476+
try {
1477+
// Try to load it with the original name
1478+
language = Translation.loadPath(name);
1479+
} catch (e) {
1480+
// If we couldn't load it, check if it wasn't the full path
1481+
name = 'select2/i18n/' + name;
1482+
language = Translation.loadPath(name);
1483+
}
1484+
1485+
languages.extend(language);
1486+
}
1487+
1488+
options.translations = languages;
1489+
} else {
1490+
options.translations = new Translations(options.language);
1491+
}
1492+
14161493
return options;
14171494
};
14181495

14191496
Defaults.prototype.reset = function () {
1420-
this.defaults = { };
1497+
this.defaults = {
1498+
language: ['select2/i18n/en']
1499+
};
14211500
};
14221501

14231502
var defaults = new Defaults();

0 commit comments

Comments
 (0)