Skip to content

Commit 175c784

Browse files
committed
Accept strings as well as functions for localization
1 parent 596cf52 commit 175c784

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

select2.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,16 @@ the specific language governing permissions and limitations under the Apache Lic
564564
function checkFormatter(formatter, formatterName) {
565565
if ($.isFunction(formatter)) return true;
566566
if (!formatter) return false;
567-
throw new Error(formatterName +" must be a function or a falsy value");
567+
if (typeof(formatter) === 'string') return true;
568+
throw new Error(formatterName +" must be a string, function, or falsy value");
568569
}
569570

570571
function evaluate(val) {
571-
return $.isFunction(val) ? val() : val;
572+
if ($.isFunction(val)) {
573+
var args = Array.prototype.slice.call(arguments, 1);
574+
return val.apply(null, args);
575+
}
576+
return val;
572577
}
573578

574579
function countResults(results) {
@@ -1634,14 +1639,14 @@ the specific language governing permissions and limitations under the Apache Lic
16341639
if (maxSelSize >=1) {
16351640
data = this.data();
16361641
if ($.isArray(data) && data.length >= maxSelSize && checkFormatter(opts.formatSelectionTooBig, "formatSelectionTooBig")) {
1637-
render("<li class='select2-selection-limit'>" + opts.formatSelectionTooBig(maxSelSize) + "</li>");
1642+
render("<li class='select2-selection-limit'>" + evaluate(opts.formatSelectionTooBig, maxSelSize) + "</li>");
16381643
return;
16391644
}
16401645
}
16411646

16421647
if (search.val().length < opts.minimumInputLength) {
16431648
if (checkFormatter(opts.formatInputTooShort, "formatInputTooShort")) {
1644-
render("<li class='select2-no-results'>" + opts.formatInputTooShort(search.val(), opts.minimumInputLength) + "</li>");
1649+
render("<li class='select2-no-results'>" + evaluate(opts.formatInputTooShort, search.val(), opts.minimumInputLength) + "</li>");
16451650
} else {
16461651
render("");
16471652
}
@@ -1651,15 +1656,15 @@ the specific language governing permissions and limitations under the Apache Lic
16511656

16521657
if (opts.maximumInputLength && search.val().length > opts.maximumInputLength) {
16531658
if (checkFormatter(opts.formatInputTooLong, "formatInputTooLong")) {
1654-
render("<li class='select2-no-results'>" + opts.formatInputTooLong(search.val(), opts.maximumInputLength) + "</li>");
1659+
render("<li class='select2-no-results'>" + evaluate(opts.formatInputTooLong, search.val(), opts.maximumInputLength) + "</li>");
16551660
} else {
16561661
render("");
16571662
}
16581663
return;
16591664
}
16601665

16611666
if (opts.formatSearching && this.findHighlightableChoices().length === 0) {
1662-
render("<li class='select2-searching'>" + opts.formatSearching() + "</li>");
1667+
render("<li class='select2-searching'>" + evaluate(opts.formatSearching) + "</li>");
16631668
}
16641669

16651670
search.addClass("select2-active");
@@ -1710,15 +1715,15 @@ the specific language governing permissions and limitations under the Apache Lic
17101715
}
17111716

17121717
if (data.results.length === 0 && checkFormatter(opts.formatNoMatches, "formatNoMatches")) {
1713-
render("<li class='select2-no-results'>" + opts.formatNoMatches(search.val()) + "</li>");
1718+
render("<li class='select2-no-results'>" + evaluate(opts.formatNoMatches, search.val()) + "</li>");
17141719
return;
17151720
}
17161721

17171722
results.empty();
17181723
self.opts.populateResults.call(this, results, data.results, {term: search.val(), page: this.resultsPage, context:null});
17191724

17201725
if (data.more === true && checkFormatter(opts.formatLoadMore, "formatLoadMore")) {
1721-
results.append("<li class='select2-more-results'>" + self.opts.escapeMarkup(opts.formatLoadMore(this.resultsPage)) + "</li>");
1726+
results.append("<li class='select2-more-results'>" + self.opts.escapeMarkup(evaluate(opts.formatLoadMore, this.resultsPage)) + "</li>");
17221727
window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
17231728
}
17241729

@@ -3042,7 +3047,7 @@ the specific language governing permissions and limitations under the Apache Lic
30423047
if(!this.opts.createSearchChoice && !choices.filter('.select2-result:not(.select2-selected)').length > 0){
30433048
if(!data || data && !data.more && this.results.find(".select2-no-results").length === 0) {
30443049
if (checkFormatter(self.opts.formatNoMatches, "formatNoMatches")) {
3045-
this.results.append("<li class='select2-no-results'>" + self.opts.formatNoMatches(self.search.val()) + "</li>");
3050+
this.results.append("<li class='select2-no-results'>" + evaluate(self.opts.formatNoMatches, self.search.val()) + "</li>");
30463051
}
30473052
}
30483053
}

0 commit comments

Comments
 (0)