Block Translation in Templates

A snippet showing how to translate dynamic text blocks in templates.


<!-- templates/account_welcome.html -->
{% load i18n %}
{% blocktrans with user_name=user.first_name login_count=user.login_count %}
Hello {{ user_name }}, you have logged in {{ login_count }} times.
{% endblocktrans %}

{% blocktrans count item_count=items|length %}
There is {{ item_count }} item in your cart.
{% plural %}
There are {{ item_count }} items in your cart.
{% endblocktrans %}
      
Explanation:
  • Use with to pass variables; use count/plural for plural forms.
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.