Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify context of translation in Django {% trans %} {% blocktrans %}?

Documentation of Django says Contextual markers are also supported by the trans and blocktrans template tags. but it not explained how to do it?

Can you help marking translation context since I have some words with several meanings.

In Python I can do in such way:

pgettext("month name", "May")
pgettext("verb", "May")

How to specify translation context in Django template?

{% blocktrans %}May{% endblocktrans %}
like image 332
Chameleon Avatar asked May 25 '13 11:05

Chameleon


People also ask

What does {% %} do in Django?

{% extends variable %} uses the value of variable . If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

What are contexts in Django?

A context processor has a simple interface: It's a Python function that takes one argument, an HttpRequest object, and returns a dictionary that gets added to the template context. Custom context processors can live anywhere in your code base.

What is trans tag in Django?

The {% trans %} template tag The {% trans %} template tag allows you to mark a string, a constant, or variable content for translation. Internally, Django executes gettext() on the given text.

What is gettext lazy in Django?

1. It will translate the string to the activated language if you defined a translation. See docs.djangoproject.com/en/4.0/topics/i18n/translation.


1 Answers

It is explained at the very end of their specific paragraphs:

https://docs.djangoproject.com/en/dev/topics/i18n/translation/#trans-template-tag

{% trans %} also supports contextual markers using the context keyword:

{% trans "May" context "month name" %}

https://docs.djangoproject.com/en/dev/topics/i18n/translation/#blocktrans-template-tag

{% blocktrans %} also supports contextual markers using the context keyword:

{% blocktrans with name=user.username context "greeting" %}Hi {{ name }}{% endblocktrans %}
like image 113
andrea.ge Avatar answered Sep 21 '22 13:09

andrea.ge