When you mark the translated string, you need to write (or get the existing) translation information for the language. Here's how it works.
Geographical restrictions
Django does not support localizing your app to a region where it has not yet been translated. In this case, it will ignore your translation files. If you want to try this and Django supports it, you will inevitably see such a mixture – mixed with your translation and from Django's own English. If your application requires you to support a region that is not in Django, you will need at least one minimal translation of the Django core.
Message file
The first step is to create an information file for a language. An information file is a text file that contains a translation string for a language and a translation of those strings. The information file is named suffix. po.
Django comes with a tool, bin/make-messages.py, which 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
Where de is the language code of the information file that is created. Here, the language code is given in local format. For example, the Portuguese language in the Brazilian region is Pt_br, and the German language in the Australian region is de_at.
This script should run in one of three places:
- The Django Project root directory.
- The root directory of your Django app.
- The Django root directory (not the Subversion check out directory, but by $PYTHONPATH link or somewhere in that path). This is only relevant when you create a translation for Django yourself.
This script iterates through your project source tree or your application source tree and extracts all the strings that are marked for translation. It creates (or updates) an information file under the Locale/lang/lc_messages directory. For the de above, it should be locale/de/lc_messages/django.po.
As a default, django-admin.py Makemessages detects every file that has an. html extension. In case you want to reload the default value, use the--extension or-e option to specify the file name extension to detect.
django-admin.py makemessages-l DE-E txt
Use the comma and/or the-e or--extension to separate multiple extension names:
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, make-messages.py will create 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; just a blank translation information file.
Do you work on Windows?
If you are using Windows and need to install the GNU GetText Common Program so that django-admin Makemessages can work, see the GetText section of the Windows section below for more information.
The. po file format is straightforward. Each. po file contains a small subset of metadata, such as a translation maintainer's contact information, and most of the file is a simple translation string and a list of mapping relationships for the corresponding language translation results.
For example, if the Django application includes a "Welcome to My Site." String to translate, like this:
_ ("Welcome to My Site.")
Then django-admin.py Makemessages will create a. po file to contain messages for the following fragment:
#: Path/to/python/module.py:23msgid "Welcome to My Site." Msgstr ""
Quick explanation:
- MsgId is a translated string that appears in the source file. Don't make any changes.
- MSGSTR is the result of the translation of the corresponding language. It was just an empty string when it was created, and you need to do it at this point. Be careful not to drop the quotation marks around the statement.
- As a convenience, each message includes a comment line prefixed with # and a msgid row, file name, and line number on top.
For longer information there is also a way to handle it. The string immediately following msgstr (or MsgId) is an empty string. Then the real content is in a few lines below it. These strings are directly linked together. Also, do not forget the spaces at the end of the string, because they are joined together without spaces.
To verify all source code and templates for the newly created translation string, and to update the information files for all languages, you can run the following command:
django-admin.py makemessages-a
Compiling information files
Once the information file is created, each time it is modified, it needs to be recompiled into a more efficient form for gettext to use. Can be done using django-admin.py compilemessages.
This tool acts on all valid. po files, creating an optimized binary. Mo file for gettext use. Under the directory where you can run django-admin.py makemessages, run django-admin.py compilemessages:
django-admin.py compilemessages
That's it. Your translation results are ready for use.
Compiling information files
Once the information file is created, each time it is modified, it needs to be recompiled into a more efficient form for gettext to use. Can be done using django-admin.py compilemessages.
This tool acts on all valid. po files, creating an optimized binary. Mo file for gettext use. Under the directory where you can run django-admin.py makemessages, run django-admin.py compilemessages:
django-admin.py compilemessages
That's it. Your translation results are ready for use.