Official text: https://docs.djangoproject.com/en/1.10/#internationalization-and-localization
The purpose of internationalization and localization is to display web pages for different users in the language and format they are most familiar with.
Django can perfectly support text translation, date, time and number formatting, and time zone.
In addition, Django has two advantages:
- Allow developers and template authors to specify which app s they should translate or format locally.
- Allow users to achieve localized display according to their preferences. Translation is based on language and formatting is based on country. This information is determined by the Accept-Language header in the browser. So far, however, the time zone has not been achieved.
To configure
In fact, the internationalization of django is very good, and the configuration is very simple.
settings.py
First, in settings, add the following:
from django.utils.translation import ugettext_lazy as _ LOCALE_PATH = { os.path.join(BASE_DIR, 'locale') } LANGUAGES = ( ('en', _('English')), ('zh-cn', _('Simplified Chinese')) ) LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True
Through the LANGUAGES execution language list, LOCALE_PATHS specifies the internationalized directory.
translate
Create a local folder under the project root directory, and then use commands to create internationalization files:
python manage.py makemessages -l zn_CN
After execution, create zh_CN/LC_MESSAGES/django.po under the local folder, which is similar to the following:
# SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2017-03-14 15:51+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <LL@li.org>\n" "Language: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" #: .\categories\admin.py:17 .\forum\admin.py:15 msgid "is visible" msgstr "" #: .\categories\models.py:18 msgid "parent category" msgstr "" #: .\categories\models.py:20 msgid "name" msgstr "" #: .\categories\models.py:21 .\covers\models.py:14 .\covers\models.py:27 #: .\covers\models.py:28 msgid "cover" msgstr "" #: .\categories\models.py:23 .\forum\models.py:36 msgid "slug" msgstr "" #: .\categories\models.py:24 msgid "description" msgstr "" #: .\categories\models.py:30 .\categories\models.py:31 .\forum\models.py:40 msgid "category" msgstr "" #: .\config\settings.py:140 msgid "English" msgstr "" #: .\config\settings.py:141 msgid "Simplified Chinese" msgstr "" #: .\covers\models.py:16 msgid "caption" msgstr "" #: .\forum\models.py:34 msgid "title" msgstr "" #: .\forum\models.py:35 msgid "body" msgstr "" #: .\forum\models.py:37 msgid "views" msgstr "" #: .\forum\models.py:38 msgid "pinned" msgstr "" #: .\forum\models.py:41 msgid "author" msgstr "" #: .\forum\models.py:42 msgid "tags" msgstr "" #: .\forum\models.py:44 .\replies\models.py:12 msgid "replies" msgstr "" #: .\forum\models.py:50 msgid "post" msgstr "" #: .\forum\models.py:51 msgid "posts" msgstr "" #: .\replies\models.py:11 msgid "reply" msgstr ""
Write down what you need to translate above. For example, name is translated into name.
Compiling translation
After all the translations have been written, they will be executed:
python manage.py compilemessages
At this point, the file zh_CN/LC_MESSAGES/django.mo will be generated, which is the final target file.