Create a language file in the Python Django framework

Source: Internet
Author: User
This article mainly introduces how to create a language file in the Python Django framework. The language code is used to represent the type of the language region. If you need a friend, refer to when you mark the translation string, you need to write (or obtain existing) the corresponding language translation information. Here is how it works.

Region restrictions

Django does not support localization of your application to a region that is not translated by itself. In this case, it will ignore your translation file. If you want to try this and Django supports it, you will inevitably see a mixture of your translations and your own English. If your application requires that you support a region that is not in Django, you need at least one least Django core translation.
Message File

The first step is to create an information file for a language. An information file is a text file that contains the translation strings of a language and the translation of these strings. The information file is suffixed with. po.

Django comes with a tool, bin/make-messages.py that completes the creation and maintenance of these files. Run the following command to create or update an information file:

django-admin.py makemessages -l de

De is the language code of the created information file. The language code is provided in local format. For example, the Portuguese in Brazil is pt_BR, and the German in Australia is de_AT.

This script should be run in one of three places:

  1. The root directory of the Django project.
  2. The root directory of your Django application.
  3. Django root directory (not the Subversion check directory, but through the $ PYTHONPATH link or somewhere in this path ). This is only relevant when you create a translation for Django.

This script traverses your project source tree or your application source tree and extracts all the strings marked for translation. It creates (or updates) an information file in the locale/LANG/LC_MESSAGES directory. For the above de, it should be locale/de/LC_MESSAGES/django. po.

By default, the django-admin.py makemessages detects every file with a. html extension. To reload the default value, use the -- extension or-e Option to specify the file extension.

django-admin.py makemessages -l de -e txt

Use commas (,) and-e or -- extension to separate multiple extensions:

django-admin.py makemessages -l de -e html,txt -e xml

When creating a JavaScript translation directory, you need to use a special Django domain: not-e js.

No gettext?

If the gettext component is not installed, the make-messages.py creates a blank file. In this case, install the gettext component or just copy the English information file (conf/locale/en/LC_MESSAGES/django. po) as a starting point. It is just a blank translation information file.

Does work on Windows?

If you are using Windows and you need to install the GNU gettext shared program so that django-admin makemessages can work, see the gettext section in the following Windows section for more information.

The. po file format is intuitive. Each. po file contains a small part of the metadata, such as the contact information of the translation maintenance personnel. Most of the content of the file is a list of simple translation strings and mappings between the Translation results of the corresponding language.

For example, if the Django application contains a "Welcome to my site." string to be translated, such:

_("Welcome to my site.")

The django-admin.py makemessages will create a. po file containing messages for the following fragments:

#: path/to/python/module.py:23msgid "Welcome to my site."msgstr ""

Quick explanation:

  • Msgid is the translation string that appears in the source file. Do not make changes.
  • Msgstr is the translation result of the corresponding language. At the time of creation, it is just a null string and you need to complete it. Do not discard the quotation marks before and after the statement.
  • For convenience, each message includes a comment row prefixed with # And locates the msgid row, file name, and row number above.

You can also process long information. The string followed by msgstr (or msgid) is an empty string. Then the real content is in the following lines. These strings are directly connected together. At the same time, do not forget the spaces at the end of the string, because they will be connected together without spaces.

To verify all source code and templates for the newly created translation string and update the information files in all languages, run the following command:

django-admin.py makemessages -a

Compile information files

After creating an information file, you need to recompile it into a more efficient form for gettext every time you modify it. You can do it using the django-admin.py compilemessages.

This tool acts on all valid. po files and creates optimized binary. mo files for gettext. Run the django-admin.py compilemessages in the directory where you can run the django-admin.py makemessages:

django-admin.py compilemessages

That's it. Your Translation results are ready for use.

Compile information files

After creating an information file, you need to recompile it into a more efficient form for gettext every time you modify it. You can do it using the django-admin.py compilemessages.

This tool acts on all valid. po files and creates optimized binary. mo files for gettext. Run the django-admin.py compilemessages in the directory where you can run the django-admin.py makemessages:

django-admin.py compilemessages

That's it. Your Translation results are ready for use.

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.