| 123 | | Using translations in Django templates works much like translations in Python |
|---|
| 124 | | code. The ``{% i18n %}`` template tag lets you use the same ``_()`` helper |
|---|
| 125 | | function as in your Python code:: |
|---|
| 126 | | |
|---|
| 127 | | <html> |
|---|
| 128 | | <title>{% i18n _('This is the title.') %}</title> |
|---|
| 129 | | <body> |
|---|
| 130 | | <p>{% i18n _('Hello %(name)s, welcome at %(site)s!') %}</p> |
|---|
| 131 | | <p>{% i18n ngettext('There is %(count)d file', 'There are %(count)d files', files|count) %}</p> |
|---|
| 132 | | </body> |
|---|
| 133 | | </html> |
|---|
| 134 | | |
|---|
| 135 | | It's not only possible to translate hard-coded strings, but the strings can |
|---|
| 136 | | contain interpolated parts, e.g. ``%(name)s``. Those parts are automatically |
|---|
| 137 | | resolved from the template context, just as they would be if you had used them |
|---|
| 138 | | in ``{{ ... }}``. But this can only resolve variables, not more complex |
|---|
| 139 | | expressions. |
|---|
| 140 | | |
|---|
| 141 | | To translate a variable value, do this:: |
|---|
| 142 | | |
|---|
| 143 | | {% i18n _(variable) %} |
|---|
| 144 | | |
|---|
| 145 | | Filters are allowed, too:: |
|---|
| 146 | | |
|---|
| 147 | | {% i18n _(variable|lower) %} |
|---|
| 148 | | |
|---|
| 149 | | Any template tag that resolves variables accepts i18n-string constants, too. |
|---|
| 150 | | |
|---|
| 151 | | Also, note you can use ``_()`` around variable names, like so:: |
|---|
| 152 | | |
|---|
| 153 | | <html> |
|---|
| 154 | | <title>{{ _('This is the title') }}</title> |
|---|
| 155 | | <body> |
|---|
| 156 | | <p>{{ _('Hello, world!') }}</p> |
|---|
| 157 | | </body> |
|---|
| 158 | | </html> |
|---|
| 159 | | |
|---|
| 160 | | This syntax is much shorter, but it doesn't allow you to use ``gettext_noop`` |
|---|
| 161 | | or ``ngettext``. |
|---|
| | 123 | Using translations in Django templates uses two template tags and a slightly |
|---|
| | 124 | different syntax than standard gettext. The ``{% trans %}`` template tag |
|---|
| | 125 | translates a constant string or a variable content:: |
|---|
| | 126 | |
|---|
| | 127 | <title>{% trans 'This is the title.' %}</title> |
|---|
| | 128 | |
|---|
| | 129 | If you only want to mark some value for translation, but translate it |
|---|
| | 130 | later from a variable, use the ``noop`` option:: |
|---|
| | 131 | |
|---|
| | 132 | <input name="field" value="{% trans "value" noop %}"/> |
|---|
| | 133 | |
|---|
| | 134 | It is not possible to use variables in this constant string. If you |
|---|
| | 135 | have variables you need to put in your translations, you have to use the |
|---|
| | 136 | ``{% blocktrans %}`` tag:: |
|---|
| | 137 | |
|---|
| | 138 | {% blocktrans %}This will have {{ value }} inside{% endblocktrans %} |
|---|
| | 139 | |
|---|
| | 140 | If your expressions are more complex (like you need to have filters applied), |
|---|
| | 141 | you need to bind them to local variables for the translation block:: |
|---|
| | 142 | |
|---|
| | 143 | {% blocktrans with value|filter as variable %} |
|---|
| | 144 | This will have {{ value }} inside |
|---|
| | 145 | {% endblocktrans %} |
|---|
| | 146 | |
|---|
| | 147 | The last variant is the pluralization form: you need to specify both the singular |
|---|
| | 148 | and plural sentence with intersparsed variables like this:: |
|---|
| | 149 | |
|---|
| | 150 | {% blocktrans count list|counted as counter %} |
|---|
| | 151 | There is only one {{ name }} object. |
|---|
| | 152 | {% plural %} |
|---|
| | 153 | There are {{ counter }} {{ name }} objects. |
|---|
| | 154 | {% endblocktrans %} |
|---|
| | 155 | |
|---|
| | 156 | Internally all block translations and inline translations are translated into |
|---|
| | 157 | the actual gettext/ngettext call. |
|---|