Django Application Deployment-multi-lingual support

Source: Internet
Author: User
Tags i18n locale django website

http://blog.csdn.net/pipisorry/article/details/47185795

If you want to implement a multi-language version of the Django website, such as two websites in English and Chinese, LZ will introduce one of the quickest ways

Configuration

settings.py has an attribute language_code = ' zh_cn ', which sets the default language of the website.

Because the properties inside the settings.py support rewriting, it is known from the official documentation that i18n support is enabled by default, use_i18n = trueuse_l10n = True

[i18n on Django's official document]

Add some middleware to support dynamic switching of languages

middleware_classes = (' Django.contrib.sessions.middleware.SessionMiddleware ', ' Django.middleware.locale.LocaleMiddleware ', ' django.middleware.common.CommonMiddleware ',)

Note: Localemiddleware must be under Sessionmiddleware, because there is a need to get a language type from the session, these Django are ready to use, very convenient.
Configure a i18n auxiliary application in the urls.py

(R ' ^i18n/', include (' django.conf.urls.i18n ')),

With this it is possible to switch languages freely, using the form of/i18n/setlang/?language=en.

http://blog.csdn.net/pipisorry/article/details/47185795


Locale Directory
After the configuration is complete,new locale directory under project directory, the style of the locale subdirectory is in a fixed format, such as:
locale/<language>/lc_messages/
If it is in Chinese, the corresponding directory is
locale/zh_cn/lc_messages/
If it is in English, it should be
locale/en/lc_messages/

In order to use the I18n,po file name in Django must be Djang.po, after compiling must be django.mo, then the content of the translation is fixed in the Po file. A typical PO file resembles the style:

# SOME Descriptive TITLE.
# Copyright (C) year the package ' S copyright HOLDER
# This file is distributed under the same license as the packagepackage.
# First AUTHOR, year.
#
#, Fuzzy
MsgId ""
Msgstr ""
"Project-id-version:package version\n"
"Report-msgid-bugs-to: \ n"
"Pot-creation-date:2006-12-21 14:00+0800\n"
"Po-revision-date:year-mo-da ho:mi+zone\n"
"Last-translator:full NAME \ n"
"Language-team:language \ n"
"Mime-version:1.0\n"
"Content-type:text/plain; Charset=utf-8\n "
"Content-transfer-encoding:8bit\n"

MsgId "Home"
Msgstr "Home"

MsgId "News"
Msgstr "News"

The format is relatively simple and is also the form of key-value pairs. If it is multi-line, you need to use the form of Msgstr "", the first line does not write things, in the following lines to write text, the results will be translated by the program automatically put the text together.

The PO files that have been written need to be edited into binary mo files before they can be used by Django, and Django uses GetText to translate, so MO format is also gettext required.

The conversion process can be done using Msgfmt-o Django.modjango.po under Linux, which is very convenient and requires downloading Poedit software under Windows.

Translation work is ready, the next is to reflect the template up, first load i18n, in the template file header to add {% load i18n%}, down for the fields that need i18n support to use {% trans ' key '%}, here is the key is MsgId, very simple.

Here's just a quick-start configuration of the Django i18n, for more detailed information, see
http://www.djangoproject.com/documentation/i18n/

from:http://blog.csdn.net/pipisorry/article/details/47185795

Ref


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Django Application Deployment-multi-lingual support

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.