Set language not working select2

后端 未结 5 2172
灰色年华
灰色年华 2021-02-18 15:44

                        
    
提交评论

  • 2021-02-18 16:26

    Additional to @SummerRain's answer, if you have loaded the language file into your page, you can use below way to set the default language for all select2, instead of one by one.

    $.fn.select2.defaults.set("language", $.fn.select2.amd.require("select2/i18n/fr"));
    

    select2 uses amd to define its dependencies as you can see from $.fn.select2.amd. "select2/i18n/fr" is the amd module name for the french language definitions.

    0 讨论(0)
  • 2021-02-18 16:28

    CDN scripts:

    <script src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/i18n/[culture-name].js"></script>
    
    0 讨论(0)
  • 2021-02-18 16:28

    If you use Webpack, you can set the default language like that (example for french) :

    require('select2');
    $.fn.select2.amd.define('select2/i18n/fr',[],require("select2/src/js/select2/i18n/fr"));
    
    0 讨论(0)
  • 2021-02-18 16:45

    Add script like this after select2 plugin

    $.fn.select2.amd.define('select2/i18n/ru',[],function () {
        // Russian
        return {
            errorLoading: function () {
                return 'Результат не может быть загружен.';
            },
            inputTooLong: function (args) {
                var overChars = args.input.length - args.maximum;
                var message = 'Пожалуйста, удалите ' + overChars + ' символ';
                if (overChars >= 2 && overChars <= 4) {
                    message += 'а';
                } else if (overChars >= 5) {
                    message += 'ов';
                }
                return message;
            },
            inputTooShort: function (args) {
                var remainingChars = args.minimum - args.input.length;
    
                var message = 'Пожалуйста, введите ' + remainingChars + ' или более символов';
    
                return message;
            },
            loadingMore: function () {
                return 'Загружаем ещё ресурсы…';
            },
            maximumSelected: function (args) {
                var message = 'Вы можете выбрать ' + args.maximum + ' элемент';
    
                if (args.maximum  >= 2 && args.maximum <= 4) {
                    message += 'а';
                } else if (args.maximum >= 5) {
                    message += 'ов';
                }
    
                return message;
            },
            noResults: function () {
              return 'Ничего не найдено';
            },
            searching: function () {
              return 'Поиск…';
            }
        };
    });
    

    set option

    $(".js-basic-multiple").select2({
        language: "ru"
    });
    
    0 讨论(0)
  • 提交回复
    热议问题