select2 4.0.0:如何使用 requirejs 更改默认语言

select2 4.0.0: How to change default language with requirejs

我使用 RequireJS 导入 javascript 库。我想更改 select2 的默认语言作为文档中的指南,但没有成功。

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

'select2' in define() 指向 select2/dist/js/select2.full.js in select2 4.0.0 package.

我是否必须手动加载语言环境文件 select2/dist/js/i18n/es.js 或执行其他操作?

此外,我想更改所有 select2 实例的默认语言设置,这样我就不需要在那里传递语言值了。

谢谢。

我创建了一些关于如何设置默认语言的 fiddle,基本上是这样的: $.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 如何使用 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/');

这里是支持的语言列表: languages select2