Use Django templates to work with string translation

Source: Internet
Author: User
Tags i18n
The Django template uses two template tags, and the syntax format is slightly different from the Python code. To make the template accessible to the tag, you need to put {% load i18n%} at the front of the template.

This {% trans%} template tag translates a constant string (enclosed in single or double quotes) or variable content:

{% trans "this is the title."%}{% trans MyVar%}

If the NOOP option is available, the variable query is valid but the translation is skipped. This is useful when the vacant content requires a future translation.

{% trans "myvar" NoOp%}

In a string with the {% trans%}, it is not possible to infiltrate a template variable. If your translation requires that the string be prefixed with a variable (placeholder placeholders), use {% Blocktrans%}:

{% Blocktrans%} This string would have {{value}} inside. {% Endblocktrans%}

Using a template filter to translate a template expression, you need to bind the expression to a local variable in the text of the translation:

{% Blocktrans with Value|filter as MyVar%} This would have {{MyVar}} inside. {% Endblocktrans%}

If you need to bind multiple expressions within a Blocktrans tag, you can separate them with and:

{% Blocktrans with Book|title as book_t and Author|title as author_t%} This was {{book_t}} by {{author_t}}{% Endblocktrans%}

To represent the contents of a single complex, you need to use the {% plural%} label between {% Blocktrans%} and {% Endblocktrans%} to specify a single plural form, for example:

{% Blocktrans Count list|length as counter%} There is only one {{name}} object. {% plural%} There is {{counter}} {{name}} objects. {% Endblocktrans%}

Its intrinsic mechanism is that all blocks and inline translations call the corresponding GetText or Ngettext.

Each of the RequestContext can access three specified translation variables:

    1. {{LANGUAGES}} is a list of a series of tuples, the first element of each tuple is the language code, and the second element is the language name in that language.
    2. As a one or two string, Language_code is the preferred language for the current user. For example: en-us. (See how Django Finds language preferences below.)
    3. Language_bidi is the description of the current region. If true, it is a right-to-left language, for example: Hebrew, Arabic. If False, it is the language written from left to right, such as: English, French, German, and so on.

If you don't need this requestcontext extension, you can use 3 tags to those values:

{% get_current_language as language_code%} {% get_available_languages as languages%} {% Get_current_language_bidi as Language_bidi%}

These tokens also require a {% load i18n%}.

The hooks in the translation can also be used in any template block tag that accepts a constant string. At this point, use the _ () expression to specify the translation string, for example:

{% Some_special_tag _ ("Page not Found") value|yesno:_ ("Yes,no")%}

In this case, both the tag and the filter two will see the translated strings, all of which do not need to be wary of translation operations.

Note:

In this example, the translation structure will miss the string "Yes,no" instead of the separate string "yes" and "no". The translated string will need to include commas so that the filter resolves the code to understand how to split the parameters. For example, a German translator might translate the string "Yes,no" to "Ja,nein" (keeping the comma intact).

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.