def get_i18n_name(lang_code):
    """Ensure lang_code is supported by Select2."""
    lower_lang = lang_code.lower()
    split_lang = lang_code.split('-')[0]
    # Use the SELECT2_TRANSLATIONS if available
    if SELECT2_TRANSLATIONS:
        if lower_lang in SELECT2_TRANSLATIONS:
            return SELECT2_TRANSLATIONS.get(lower_lang)
        elif split_lang in SELECT2_TRANSLATIONS:
            return SELECT2_TRANSLATIONS.get(split_lang)
    # Otherwise fallback to manually checking if the static file exists
    if finders.find('admin/js/vendor/select2/i18n/%s.js' % lang_code):
        return lang_code
    elif finders.find('admin/js/vendor/select2/i18n/%s.js' % split_lang):
        return lang_code.split('-')[0]
Exemple #2
0
def get_i18n_name(lang_code):
    """Ensure lang_code is supported by Select2."""
    lower_lang = lang_code.lower()
    split_lang = lang_code.split('-')[0]
    # Use the SELECT2_TRANSLATIONS if available
    if SELECT2_TRANSLATIONS:
        if lower_lang in SELECT2_TRANSLATIONS:
            return SELECT2_TRANSLATIONS.get(lower_lang)
        elif split_lang in SELECT2_TRANSLATIONS:
            return SELECT2_TRANSLATIONS.get(split_lang)
    # Otherwise fallback to manually checking if the static file exists
    if finders.find('%s%s.js' % (I18N_PATH, lang_code)):
        return lang_code
    elif finders.find('%s%s.js' % (I18N_PATH, split_lang)):
        return lang_code.split('-')[0]
Exemple #3
0
    def _get_media(self):
        """
        Construct Media as a dynamic property.

        .. Note:: For more information visit
            https://docs.djangoproject.com/en/stable/topics/forms/media/#media-as-a-dynamic-property
        """
        lang = get_language()
        select2_js = (settings.SELECT2_JS,) if settings.SELECT2_JS else ()
        select2_css = (settings.SELECT2_CSS,) if settings.SELECT2_CSS else ()
        django_select2_js = (
            settings.SELECT2_DJANGO_SELECT2_JS, ) if settings.SELECT2_DJANGO_SELECT2_JS else ()

        i18n_name = SELECT2_TRANSLATIONS.get(lang)
        if i18n_name not in settings.SELECT2_I18N_AVAILABLE_LANGUAGES:
            i18n_name = None

        i18n_file = (
            ("%s/%s.js" % (settings.SELECT2_I18N_PATH, i18n_name),) if i18n_name else ()
        )

        return forms.Media(
            js=select2_js + i18n_file + django_select2_js,
            css={"screen": select2_css},
        )
Exemple #4
0
    def _get_media(self):
        """
        Construct Media as a dynamic property.

        .. Note:: For more information visit
            https://docs.djangoproject.com/en/stable/topics/forms/media/#media-as-a-dynamic-property
        """
        lang = get_language()
        i18n_name = None
        select2_js = (settings.SELECT2_JS, ) if settings.SELECT2_JS else ()
        select2_css = (settings.SELECT2_CSS, ) if settings.SELECT2_CSS else ()

        try:
            from django.contrib.admin.widgets import SELECT2_TRANSLATIONS
            i18n_name = SELECT2_TRANSLATIONS.get(lang)
            if i18n_name not in settings.SELECT2_I18N_AVAILABLE_LANGUAGES:
                i18n_name = None
        except ImportError:
            # TODO: select2 widget feature needs to be backported into Django 1.11
            try:
                i = [
                    x.lower()
                    for x in settings.SELECT2_I18N_AVAILABLE_LANGUAGES
                ].index(lang)
                i18n_name = settings.SELECT2_I18N_AVAILABLE_LANGUAGES[i]
            except ValueError:
                pass

        i18n_file = (
            '%s/%s.js' %
            (settings.SELECT2_I18N_PATH, i18n_name), ) if i18n_name else ()

        return forms.Media(js=select2_js + i18n_file +
                           ('django_select2/django_select2.js', ),
                           css={'screen': select2_css})
    def media(self):
        media = super().media

        i18n_file = None
        i18n_name = SELECT2_TRANSLATIONS.get(get_language(), None)
        if i18n_name:
            i18n_file = 'admin/js/vendor/select2/i18n/%s.js' % i18n_name

        extra_js = [
            'admin/js/vendor/jquery/jquery.js',
            'admin/js/vendor/select2/select2.full.js',
        ]
        if i18n_file:
            extra_js.append(i18n_file)
        extra_js.extend([
            'admin/js/jquery.init.js',
            'admin/js/autocomplete.js',
            'admin/js/autocomplete_filter.js',
        ])
        extra_css = [
            'admin/css/vendor/select2/select2.css',
            'admin/css/autocomplete.css',
        ]
        if django.VERSION >= (2, 2, 0, 'final', 0):
            media._js_lists.append(extra_js)
            media._css_lists.append({'screen': extra_css})
        else:
            media._js = OrderedSet(extra_js + media._js)
            media._css.setdefault('screen', [])
            media._css['screen'].extend(extra_css)
        return media
 def media(self):
     extra = '' if settings.DEBUG else '.min'
     i18n_name = SELECT2_TRANSLATIONS.get(translation.get_language())
     i18n_file = ('admin/js/vendor/select2/i18n/%s.js' %
                  i18n_name, ) if i18n_name else ()
     return forms.Media(
         js=(
             'admin/js/vendor/jquery/jquery%s.js' % extra,
             'admin/js/vendor/select2/select2.full%s.js' % extra,
         ) + i18n_file + (
             'admin/js/jquery.init.js',
             'autocomplete_light/jquery.init.js',
             'autocomplete_light/autocomplete.init.js',
             'admin/js/autocomplete.js',
             'autocomplete_light/forward.js',
             'autocomplete_light/select2.js',
         ),
         css={
             'screen': (
                 'admin/css/vendor/select2/select2%s.css' % extra,
                 'admin/css/autocomplete.css',
                 'autocomplete_light/select2.css',
             ),
         },
     )
    def media(self):
        """Return JS/CSS resources for the widget."""
        extra = '' if settings.DEBUG else '.min'
        i18n_name = SELECT2_TRANSLATIONS.get(translation.get_language())
        i18n_file = ('admin/js/vendor/select2/i18n/%s.js' %
                     i18n_name, ) if i18n_name else ()

        return forms.Media(
            js=(
                # dlawrence - see https://github.com/yourlabs/django-autocomplete-light/issues/1079#issuecomment-453395714
                #'admin/js/vendor/jquery/jquery%s.js' % extra,
                'autocomplete_light/jquery.init.js',
                'admin/js/vendor/select2/select2.full%s.js' % extra,
            ) + i18n_file + (
                'autocomplete_light/autocomplete.init.js',
                'autocomplete_light/forward.js',
                'autocomplete_light/select2.js',
                'autocomplete_light/jquery.post-setup.js',
            ),
            css={
                'screen': (
                    'admin/css/vendor/select2/select2%s.css' % extra,
                    'admin/css/autocomplete.css',
                    'autocomplete_light/select2.css',
                ),
            },
        )
 def media(self):
     extra = "" if settings.DEBUG else ".min"
     i18n_name = SELECT2_TRANSLATIONS.get(get_language())
     i18n_file = (("admin/js/vendor/select2/i18n/%s.js" %
                   i18n_name, ) if i18n_name else ())
     return forms.Media(
         js=(
             "admin/js/vendor/jquery/jquery%s.js" % extra,
             "admin/js/vendor/select2/select2.full%s.js" % extra,
         ) + i18n_file + ("admin/js/jquery.init.js",
                          "admin/gestion/hierarchical-select.js"),
         css={
             "screen": ("admin/css/vendor/select2/select2%s.css" % extra, ),
         },
     )
Exemple #9
0
 class Media:
     extra = '' if settings.DEBUG else '.min'
     i18n_name = SELECT2_TRANSLATIONS.get(get_language())
     i18n_file = ('admin/js/vendor/select2/i18n/%s.js' %
                  i18n_name, ) if i18n_name else ()
     js = ('admin/js/vendor/select2/select2.full%s.js' %
           extra, ) + i18n_file + (
               'admin/js/jquery.init.js',
               'admin/js/autocomplete.js',
               'js/autocomplete_filter_qs.js',
           )
     css = {
         'screen': (
             'admin/css/vendor/select2/select2%s.css' % extra,
             'admin/css/autocomplete.css',
             'css/autocomplete-fix.css',
         ),
     }
Exemple #10
0
    def _get_media(self):
        """
        Construct Media as a dynamic property.

        .. Note:: For more information visit
            https://docs.djangoproject.com/en/stable/topics/forms/media/#media-as-a-dynamic-property
        """
        lang = get_language()
        select2_js = (settings.SELECT2_JS,) if settings.SELECT2_JS else ()
        select2_css = (settings.SELECT2_CSS,) if settings.SELECT2_CSS else ()

        i18n_name = SELECT2_TRANSLATIONS.get(lang)
        if i18n_name not in settings.SELECT2_I18N_AVAILABLE_LANGUAGES:
            i18n_name = None

        i18n_file = ('%s/%s.js' % (settings.SELECT2_I18N_PATH, i18n_name),) if i18n_name else ()

        return forms.Media(
            js=select2_js + i18n_file + ('django_select2/django_select2.js',),
            css={'screen': select2_css}
        )
Exemple #11
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.i18n_name = SELECT2_TRANSLATIONS.get(get_language())
Exemple #12
0
 def __init__(self, admin_site, attrs=None, choices=(), using=None):
     self.admin_site = admin_site
     self.db = using
     self.choices = choices
     self.attrs = {} if attrs is None else attrs.copy()
     self.i18n_name = SELECT2_TRANSLATIONS.get(get_language())