Issue with AJAX, language and noresults

58 views
Skip to first unread message

carlo.coc...@gmail.com

unread,
Mar 9, 2017, 5:48:20 PM3/9/17
to select2
I'm currently using the latest version of Select2 4.0.3. I am using the French language from i18n/fr.js

The issue is, when I include the language: option to use the noResults paramter, the language reverts back to the English language. If I remove:

escapeMarkup: function (markup) { return markup; },
language: {
noResults: function (term) {
return "<a href=/'http://google.com/'>Add</a>";
}
},

it remains in the French language. I tried to initialize the default language using $.fn.select2.defaults.set('language', 'fr'); and it still remains in the English language. Is there a value I can pass in the language: parameter that can init the French language? I saw the option to use language: 'fr' but I need to include the noResults parameter.

Please help.


<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/i18n/fr.js"></script>

$('form#form-edit select').select2({

ajax: {
url: site_url + '/user/ajax_get_json',
type: 'POST',
cache: false,
dataType: 'json',
delay: 250,

data: function (params) {

// Merge object2 into object1, recursively
var query = $.extend( {}, params, items );
// Query paramters will be ?search=[term]&page=[page]
return query;
},
processResults: function (data) {

return {
results: data
};
}
}

});

carlo.coc...@gmail.com

unread,
Mar 15, 2017, 7:24:21 PM3/15/17
to select2, carlo.coc...@gmail.com
After playing with the library itself, I noticed that regardless of loading the language and setting the language default, the language never gets set because

if ($.isArray(options.language))

is never true. It always ends up in the default language. I decided to set the default language in the ELSE as so:

else {
var baseTranslation = Translation.loadPath(
//this.defaults.amdLanguageBase + 'en'
this.defaults.amdLanguageBase + this.defaults.language
);
var customTranslation = new Translation(options.language);

customTranslation.extend(baseTranslation);

options.translations = customTranslation;
}

Now I can set the language default before calling select2() and will load the proper language.

Not sure if this is a bug or an issue but I noticed other developers asking about the same issue with no solution when using the:

language: {
noResults: function (term) {
return "<a href=/'add/user'>Add</a>";
}
}

instead of:

language: 'fr'

If anyone has any comments or feedback, please feel free to post below. Thanks

Reply all
Reply to author
Forward
0 new messages