Skip to main content

Babel Tags

Optional tags provided by the liquid-babel package.

translate

New in Liquid Babel version 0.3.0

{% translate
[context: <string>]
[, count: <number>]
[, <identifier>: <object> [| <filter>] ... ] %}
<text,variable> ...
[ {% plural %} <text,variable> ... ]
{% endtranslate %}

Render the localized translation of a message. If a German Translations object is found in the current render context:

{% translate %}
Hello, World!
{% endtranslate %}
output
Hallo Welt!

If a {% plural %} block follows the message text and the count argument is considered plural, the {% plural %} block will be rendered instead.

{% translate count: 2 %}
Hello, World!
{% plural %}
Hello, Worlds!
{% endtranslate %}
output
Hallo Welten!

Keyword arguments are used to populate translatable message variables. These variables look like Liquid output statements, but can not use dotted or bracketed property/attribute access, or filters.

{% translate you: 'Sue' %}
Hello, {{ you }}!
{% endtranslate %}
output
Hallo Sue!

Keyword arguments can use simple, no-argument filters like size and capitalize.

{% translate you: 'Sue' | captialize %}
Hello, {{ you }}!
{% endtranslate %}
output
Hallo SUE!

Options

The included translate tag can be customized by subclassing TranslateTag and overriding class attributes and/or methods. This example changes its name from translate to trans and disables support for filtered arguments.

from liquid import Environment
from liquid_babel.tags.translate import TranslateTag

class MyTranslateTag(TranslateTag):
name = "trans"
end = "endtrans"
simple_filters = False

env = Environment()
env.add_tag(MyTranslateTag)

This table shows configurable TranslateTag class attributes/property.

PropertyTypeDescriptionDefault
namestrThe name of the tag that starts the block, as used by template authors."translate"
endstrThe name of the tag that ends the block, as used by template authors."endtranslate"
plural_namestrThe name of the tag that starts the plural block, as used by template authors."plural"
simple_filtersboolIf True, allow simple, no-argument filters to be used with keyword arguments.True
trim_messagesboolIf True, normalize message whitespace.True
node_classast.NodeThe node to return from TranslateTag.parse().TranslateNode

Further customization can be achieved by extending TranslateNode and overriding resolve_count(), resolve_message_context() and/or gettext().