Skip to content

Commit

Permalink
add context to templatetag ‘trans’ for fine-grained translations
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed Apr 27, 2020
1 parent 8e8ba32 commit 9863abb
Show file tree
Hide file tree
Showing 22 changed files with 132 additions and 201 deletions.
67 changes: 67 additions & 0 deletions shop/search/documents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.template.loader import select_template
from django.utils.html import strip_tags

from django_elasticsearch_dsl import fields, Document

from shop.models.product import ProductModel


# products = Index('products')
# products.settings(
# number_of_shards=1,
# number_of_replicas=0
# )


# @products.document
class ProductDocument(Document):
product_codes = fields.KeywordField(
multi=True,
)

product_type = fields.TextField(
attr='product_type',
)

body = fields.TextField()

class Django:
model = ProductModel
fields = ['id', 'product_name']

def prepare(self, instance):
data = super().prepare(instance)
return data

def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(active=True)

def prepare_product_codes(self, instance):
variants = instance.get_product_variants()
product_codes = [
v.product_code for v in variants if isinstance(getattr(v, 'product_code', None), str)
]
if isinstance(getattr(instance, 'product_code', None), str):
product_codes.append(instance.product_code)
return product_codes

def prepare_body(self, instance):
"""
Create a textual representation of the product's instance to be used by Elasticsearch for
creating a full text search index.
"""
app_label = instance._meta.app_label.lower()
params = [
(app_label, instance.product_model),
(app_label, 'product'),
('shop', 'product'),
]
template = select_template(['{0}/search/indexes/{1}.txt'.format(*p) for p in params])
body = template.render({'product': instance})
print("------------")
print(strip_tags(body))
return strip_tags(body)
136 changes: 0 additions & 136 deletions shop/search/indexes.py

This file was deleted.

2 changes: 1 addition & 1 deletion shop/templates/shop/breadcrumb/catalog.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ol class="breadcrumb">
{% show_breadcrumb 0 "bootstrap4/menu/breadcrumb.html" %}
<li class="ml-auto d-none d-sm-inline-block">
{% trans "Found Products" %}: <span class="badge badge-primary" djng-bind-if="catalog.count">{{ paginator.count }}</span>
{% trans "Found Products" context "breadcrumb" %}: <span class="badge badge-primary" djng-bind-if="catalog.count">{{ paginator.count }}</span>
</li>
</ol>
</nav>
Expand Down
10 changes: 5 additions & 5 deletions shop/templates/shop/cart/editable.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% block shop-cart-head %}
<div class="row lead text-muted" ng-if="cart.num_items==0">
<div class="col">
{% trans "There are no items in the cart." %}
{% trans "There are no items in the cart." context "cart" %}
</div>
</div>
{% endblock shop-cart-head %}
Expand Down Expand Up @@ -47,13 +47,13 @@
<div class="row">
<div class="col mb-2">
<button type="button" class="ml-auto btn btn-sm btn-secondary btn-block shop-item-control" ng-click="deleteCartItem(cart_item)">
<span class="fa fa-trash-o mr-2"></span>{% trans "Remove" %}
<span class="fa fa-trash-o mr-2"></span>{% trans "Remove" context "cart" %}
</button>
</div>
{% if shop_watch_list_url %}
<div class="col mb-2">
<button type="button" class="ml-auto btn btn-sm btn-info btn-block shop-item-control" ng-click="watchCartItem(cart_item)">
<span class="fa fa-heart-o mr-2"></span>{% trans "Watch Item" %}
<span class="fa fa-heart-o mr-2"></span>{% trans "Watch Item" context "cart" %}
</button>
</div>
{% endif %}
Expand All @@ -70,7 +70,7 @@
<div class="col-12 col-md-6 order-md-last border-top">
<div class="row py-2 lead">
<div class="col text-right text-nowrap">
{% trans "Subtotal" %}
{% trans "Subtotal" context "cart" %}
</div>
<div class="col text-right text-nowrap" ng-bind="cart.subtotal">
<!-- Subtotal -->
Expand All @@ -81,7 +81,7 @@
<div class="col text-right text-nowrap" ng-bind="extra_row.amount"></div>
</div>
<div class="row py-2 shop-double-border lead font-weight-bold">
<div class="col text-right text-nowrap">{% trans "Total" %}</div>
<div class="col text-right text-nowrap">{% trans "Total" context "cart" %}</div>
<div class="col text-right text-nowrap" ng-bind="cart.total">
<!-- Total -->
</div>
Expand Down
6 changes: 3 additions & 3 deletions shop/templates/shop/cart/static.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% if cart.num_items == 0 %}
<div class="row lead text-muted py-1">
<div class="col">
{% trans "There are no items in the cart" %}
{% trans "There are no items in the cart." context "cart" %}
</div>
</div>
{% endif %}
Expand Down Expand Up @@ -48,7 +48,7 @@
<div class="col-12 col-md-6 border-top">
<div class="row py-2 lead">
<div class="col-6 text-right text-nowrap">
{% trans "Subtotal" %}
{% trans "Subtotal" context "cart" %}
</div>
<div class="col-4 col-md-6 text-right text-nowrap" djng-bind-if="cart.subtotal">
{{ cart.subtotal }}
Expand All @@ -65,7 +65,7 @@
<div class="col-4 col-md-6 text-right text-nowrap" ng-bind="extra_row.amount"></div>
</div>
<div class="row py-2 shop-double-border lead font-weight-bold">
<div class="col-6 text-right text-nowrap">{% trans "Total" %}</div>
<div class="col-6 text-right text-nowrap">{% trans "Total" context "cart" %}</div>
<div class="col-4 col-md-6 text-right text-nowrap" djng-bind-if="cart.total">
{{ cart.total }}
</div>
Expand Down
6 changes: 3 additions & 3 deletions shop/templates/shop/cart/summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

<ul class="list-group">
<li class="list-group-item">
<span class="pull-left">{% trans "Number of items" %}:</span>
<span class="pull-left">{% trans "Number of items" context "cart" %}:</span>
<span class="pull-right font-weight-bold" djng-bind-if="cart.num_items">{{ cart.num_items }}</span>
</li>
<li class="list-group-item">
<span class="pull-left">{% trans "Subtotal" %}:</span>
<span class="pull-left">{% trans "Subtotal" context "cart" %}:</span>
<span class="pull-right font-weight-bold" djng-bind-if="cart.subtotal">{{ cart.subtotal }}</span>
</li>
{% for extra_row in cart.extra_rows %}
Expand All @@ -20,7 +20,7 @@
<span class="pull-right font-weight-bold" ng-bind="extra_row.amount"></span>
</li>
<li class="list-group-item list-group-item-secondary lead font-weight-bold">
<span class="pull-left">{% trans "Total" %}:</span>
<span class="pull-left">{% trans "Total" context "cart" %}:</span>
<span class="pull-right" djng-bind-if="cart.total">{{ cart.total }}</span>
</li>
</ul>
6 changes: 3 additions & 3 deletions shop/templates/shop/cart/watch.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{% block shop-watch-head %}
<div class="row lead text-muted" ng-if="cart.items.length==0">
<div class="col">
{% trans "You are perfectly happy!" %}
{% trans "You are perfectly happy!" context "cart" %}
</div>
</div>
{% endblock shop-watch-head %}
Expand All @@ -26,10 +26,10 @@
{% block shop-watch-item-controls %}
<div class="col-4 col-md-6">
<button type="button" class="ml-auto btn btn-sm btn-secondary btn-block shop-item-control" ng-click="deleteWatchItem(cart_item)">
<span class="fa fa-trash-o mr-2"></span>{% trans "Remove" %}
<span class="fa fa-trash-o mr-2"></span>{% trans "Remove" context "cart" %}
</button>
<button type="button" class="ml-auto btn btn-sm btn-info btn-block shop-item-control" ng-click="addWatchItem(cart_item)">
<span class="fa fa-cart-arrow-down mr-2"></span>{% trans "Add to cart" %}
<span class="fa fa-cart-arrow-down mr-2"></span>{% trans "Add to cart" context "cart" %}
</button>
</div>
{% endblock %}
Expand Down
10 changes: 5 additions & 5 deletions shop/templates/shop/catalog/available-product-add2cart.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{% block available-quantity-in-stock %}
<div class="d-flex flex-column">
<span class="label">{% trans "Available" %}</span>
<span class="label">{% trans "Available" context "catalog" %}</span>
<div class="lead mt-1" ng-cloak>
{% verbatim %}{{ context.availability.quantity }}{% endverbatim %}
</div>
Expand All @@ -12,21 +12,21 @@

{% block change-purchasing-quantity %}
<div class="d-flex flex-column w-25">
<label for="quantity">{% trans "Quantity" %}</label>
<label for="quantity">{% trans "Quantity" context "catalog" %}</label>
<div class="form-group" ng-show="context.availability.quantity">
<input class="form-control" name="quantity" type="number" min="1" max="{% verbatim %}{{ context.availability.quantity }}{% endverbatim %}" ng-model="context.quantity" ng-model-options="{{ ADD2CART_NG_MODEL_OPTIONS }}" ng-change="updateContext()" />
</div>
<div class="lead mt-1" ng-hide="context.availability.quantity" ng-cloak>
{% trans "Currently not available." %}
{% trans "Currently not available." context "catalog" %}
</div>
</div>
{% endblock %}

{% block limited-availability %}
<div class="text-muted text-left pr-2" ng-show="context.availability.sell_short" ng-cloak>
<small>{% trans "Deliverable from {{ context.availability.earliest|date:'d. MMM.' }} onwards." %}</small>
<small>{% trans "Deliverable from {{ context.availability.earliest|date:'d. MMM.' }} onwards." context "catalog" %}</small>
</div>
<div class="text-muted text-left pr-2" ng-show="context.availability.limited_offer" ng-cloak>
<small>{% trans "Available no later than {{ context.availability.latest|date:'d. MMM. H:mm' }}." %}</small>
<small>{% trans "Available no later than {{ context.availability.latest|date:'d. MMM. H:mm' }}." context "catalog" %}</small>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion shop/templates/shop/catalog/bsp-scrollpanel.tmpl.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load i18n static sekizai_tags sass_tags %}
{% load static sekizai_tags sass_tags %}
{% addtoblock "css" %}<link href="{% sass_src 'shop/css/product-gallery.scss' %}" rel="stylesheet" type="text/css" />{% endaddtoblock %}
{% addtoblock "js" %}<script src="{% static 'node_modules/angular-bootstrap-plus/src/scrollpanel/scrollpanel.js' %}" type="text/javascript"></script>{% endaddtoblock %}
{% add_data "ng-requires" "bs-plus.scrollpanel" %}
Expand Down
2 changes: 1 addition & 1 deletion shop/templates/shop/catalog/list.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load i18n static sekizai_tags sass_tags %}
{% load static sekizai_tags sass_tags %}

{% addtoblock "js" %}<script src="{% static 'shop/js/utils.js' %}" type="text/javascript"></script>{% endaddtoblock %}
{% addtoblock "js" %}<script src="{% static 'shop/js/catalog.js' %}" type="text/javascript"></script>{% endaddtoblock %}
Expand Down
Loading

0 comments on commit 9863abb

Please sign in to comment.