Django

Code

Changeset 1063

Show
Ignore:
Timestamp:
11/03/05 06:01:16 (3 years ago)
Author:
hugo
Message:

i18n: updated translation documentation for new syntax

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/i18n/docs/translation.txt

    r1058 r1063  
    121121=============================== 
    122122 
    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``. 
     123Using translations in Django templates uses two template tags and a slightly 
     124different syntax than standard gettext. The ``{% trans %}`` template tag 
     125translates a constant string or a variable content:: 
     126 
     127    <title>{% trans 'This is the title.' %}</title> 
     128 
     129If you only want to mark some value for translation, but translate it 
     130later from a variable, use the ``noop`` option:: 
     131 
     132    <input name="field" value="{% trans "value" noop %}"/> 
     133 
     134It is not possible to use variables in this constant string. If you 
     135have 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 
     140If your expressions are more complex (like you need to have filters applied), 
     141you 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 
     147The last variant is the pluralization form: you need to specify both the singular 
     148and 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 
     156Internally all block translations and inline translations are translated into 
     157the actual gettext/ngettext call. 
    162158 
    163159Each ``DjangoContext`` has access to two translation-specific variables: 
     
    167163    * ``LANGUAGE_CODE`` is the current user's preferred language, as a string. 
    168164      Example: ``en-us``. (See "How language preference is discovered", below.) 
     165 
     166If you don't use the ``DjangoContext`` extension, you can get those values with 
     167two tags:: 
     168 
     169    {% get_current_language as LANGUAGE_CODE %} 
     170    {% get_available_languages as LANGUAGES %} 
     171 
     172All tags live in the ``i18n`` tag library, so you need to specify 
     173``{% load i18n %}`` in the head of your template to make use of them. 
    169174 
    170175The ``setlang`` redirect view