{% set _select2_locales = ['ar', 'az', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'gl', 'he', 'hi', 'hr', 'hu', 'id', 'is', 'it', 'ja', 'km', 'ko', 'lt', 'lv', 'mk', 'ms', 'nb', 'nl', 'pl', 'pt-BR', 'pt', 'ro', 'ru', 'sk', 'sr-Cyrl', 'sr', 'sv', 'th', 'tr', 'uk', 'vi', 'zh-CN', 'zh-TW'] %}
{% set _app_language = app.request.locale|split('-')|first|split('_')|first %}
{% set _select2_locale = app.request.locale in _select2_locales
    ? app.request.locale
    : _app_language in _select2_locales ? _app_language : 'en'
%}

<script src="{{ asset('bundles/easyadmin/select2/i18n/' ~ _select2_locale ~ '.js') }}"></script>
<script type="text/javascript">
  $(function() {
    // Select2 widget is only enabled for the <select> elements which
    // explicitly ask for it

    function init() {
      $('form select[data-widget="select2"]').select2({
        theme: 'bootstrap',
        language: '{{ _select2_locale }}'
      });
    }

    $(document).on('easyadmin.collection.item-added', init);
    init();
  });
</script>
