summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorDavid Lord <davidism@gmail.com>2021-12-26 11:29:39 -0700
committerDavid Lord <davidism@gmail.com>2021-12-26 11:51:54 -0700
commitb6f50df85b7b26f7e79d26b455db9b55d2a21d0c (patch)
tree9cef366d29b3435e20fc78941cbab8acdea94d96 /docs
parent896a62135bcc151f2997e028c5125bec2cb2431f (diff)
downloadjinja2-b6f50df85b7b26f7e79d26b455db9b55d2a21d0c.tar.gz
specify context for translation block
Diffstat (limited to 'docs')
-rw-r--r--docs/templates.rst30
1 files changed, 27 insertions, 3 deletions
diff --git a/docs/templates.rst b/docs/templates.rst
index 89958b8..9071ad6 100644
--- a/docs/templates.rst
+++ b/docs/templates.rst
@@ -1732,11 +1732,35 @@ to disable it for a block.
.. versionadded:: 2.10
The ``trimmed`` and ``notrimmed`` modifiers have been added.
+If the translation depends on the context that the message appears in,
+the ``pgettext`` and ``npgettext`` functions take a ``context`` string
+as the first argument, which is used to select the appropriate
+translation. To specify a context with the ``{% trans %}`` tag, provide
+a string as the first token after ``trans``.
+
+.. code-block:: jinja
+
+ {% trans "fruit" %}apple{% endtrans %}
+ {% trans "fruit" trimmed count -%}
+ 1 apple
+ {%- pluralize -%}
+ {{ count }} apples
+ {%- endtrans %}
+
+.. versionadded:: 3.1
+ A context can be passed to the ``trans`` tag to use ``pgettext`` and
+ ``npgettext``.
+
It's possible to translate strings in expressions with these functions:
-- ``gettext``: translate a single string
-- ``ngettext``: translate a pluralizable string
-- ``_``: alias for ``gettext``
+- ``_(message)``: Alias for ``gettext``.
+- ``gettext(message)``: Translate a message.
+- ``ngettext(singluar, plural, n)``: Translate a singular or plural
+ message based on a count variable.
+- ``pgettext(context, message)``: Like ``gettext()``, but picks the
+ translation based on the context string.
+- ``npgettext(context, singular, plural, n)``: Like ``npgettext()``,
+ but picks the translation based on the context string.
You can print a translated string like this: