Paul Z.
Paul Z.

Reputation: 905

select2 4.0.0: How to change default language with requirejs

I use RequireJS for importing javascript libraries. I wanted to change default language for select2 as guide in documentation, but no luck.

define(['jquery', 'select2'], function($) {
      $(".select2").select2({
           language: 'es'
      });
});

'select2' in define() is pointing select2/dist/js/select2.full.js in select2 4.0.0 package.

Do I have to manually load locale file select2/dist/js/i18n/es.js or do something else?

In addition, I'd like to change default language setting for all select2 instance, so that I don't need to pass language value there.

Thanks.

Upvotes: 4

Views: 9270

Answers (1)

Legends
Legends

Reputation: 22702

I created a little fiddle on how to set the default language, basically like this: $.fn.select2.defaults.set('language', 'jp');

The language does not have to be defined when Select2 is being initialized, but instead can be defined in the [lang] attribute of any parent elements as [lang="es"].

Here is explained how language files are loaded using require.js.

How do I tell Select2 where to look for modules? For most AMD and CommonJS setups, the location of the data files used by Select2 will be automatically determined and handled without you needing to do anything.

If you are using Select2 in a build environment where preexisting module names are changed during a build step, Select2 may not be able to find optional dependencies or language files. You can manually set the prefixes to use for these files using the amdBase and amdLanugageBase options.

  1. $.fn.select2.defaults.set('amdBase', 'select2/');
  2. $.fn.select2.defaults.set('amdLanguageBase', 'select2/i18n/');

Here a list of supported languages: languages select2

Upvotes: 5

Related Questions