How to localize django-filter labels using django-parler

59 Views Asked by At

I am developing an e-commerce website and I am using django-filter to filter products by size, category, color, price. However I can't find a way to translate labels for filters using django-parler, I managed to translate choices for ascending and descending values but I am stuck here. Here is my filters.py

lass ProductFilter(django_filters.FilterSet):

    CHOICES = (
        ('ascending', _('Ascending')),
        ('descending', _('Descending'))
    )

    price = django_filters.RangeFilter(
                widget=django_filters.widgets.RangeWidget(attrs={"class":"price-search"}))
    ordering = django_filters.ChoiceFilter(label='Sort', choices = CHOICES, method='filter_by_order')


    class Meta:
        model = Product
        fields = ['size', 'color', 'category']
            

    def filter_by_order(self, queryset, name, value):
        expression = 'price' if value == 'ascending' else '-price'
        return queryset.order_by(expression)

also HTML:

    <section class="category-products">
      <div class="container">
        <div class="category-wrapper">
        {% for product in filtered_products %}
            <div class="category-item-block">
              <div class="category-item">
                <div class="category-item-image">
                   <div class="img-wrapper">
                    <a href="{% url 'product' product.slug %}"><img src="  {{product.thumbnail.url}}" alt="" /></a>
                  </div>
                  <h2>{{product.name}}</h2>
                </div>
                <div class="category-item-info">
                  <p>{{product.short_description}}</p>
                </div>
                <div class="category-item-price">
                  <p class="price">{{product.price}} AMD</p>
                  <p class="price-monthly">{{product.price_monthly}} AMD / {% trans "monthly" %}</p>
                </div>
                <div class="buy">
                  <a href="{% url 'buy' product.id %}" class="buy-now">
                    <p>{% trans "Buy Now" %}</p>
                  </a>
                  <div class="add-cart">
 
                    
                      <div class="img-wrapper">                   
                        <button hx-get="{% url 'add_to_cart' product.id %}"
                        hx-target="#menu-cart-button"
                        hx-swap="outerHTML" >
                        <img
                          class="cart-catalog"
                          src="{% static 'img\add-to-cart.svg' %}"
                          alt=""
                        />
                      </button>
                      </div>

                  </div>
                </div>
              </div>
            </div>
        {% endfor %}
        <div class="load-more">
          <button> {% trans "Load More" %} </button>
        </div>
      </div>
      <hr>
      </div>
      </div>
    </section>

I tried to add gettext_lazy to fields, but it didn't work

1

There are 1 best solutions below

0
On

i offer you to use my translation system DJANGO-TOF 2.ver

It is build on ideas Parler, Hvad and Model translation.

https://github.com/danilovmy/django-tof

i write my own system , because parler is outdated and not support the new versions of Django.