基础环境介绍

IDE我用的pycharm

Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Django 1.11

开启国际化的支持,需要在settings.py文件中设置

MIDDLEWARE_CLASSES = (
'django.middleware.locale.LocaleMiddleware',
)

LANGUAGE_CODE = 'en'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True


LANGUAGES = (
('en', ('English')),
('zh-cn', ('中文简体')),
('zh-tw', ('中文繁體')),
)

#翻译文件所在目录,需要手工创建
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)

TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.i18n",
)
#注意:Django 1.9 及以上版本中,语言的代码发生变化(详情链接:github, django ticket,如下
LANGUAGES = (
 ('en', ('English')),
 ('zh-hans', ('中文简体')),
 ('zh-hant', ('中文繁體')),
)
#langues我没配置也没什么问题

home.html页面,本文直接用root url返回的一个html template

html页面内容如下

{% load i18n %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% trans "wis" %}
<h1>hello</h1>
<h1>{% trans "god" %}</h1>
</body>
</html>

现在我的目录结构如下:

bogon:django18 hongzhi.wang$ tree .
.
├── app01
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── day1
│   ├── __init__.py
│   └── quip.py
├── db.sqlite3
├── django18
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── locale
├── manage.py
└── templates
    ├── home.html

生成国际化需要的文件(注意这时locals目录已经建好)

生成po文件

django-admin makemessages -l zh

然后目录如下:

mojideMacBook-Pro-2:apps hongzhi.wang$ tree locale/
locale/
└── zh
    └── LC_MESSAGES
        ├── django.mo
        └── django.po

这时django.po文件如下:

# 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-07-26 11:08+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"

#: app01/views.py:7
msgid "Welcome to my site."
msgstr ""

#: templates/home.html:9
msgid "wis"
msgstr ""

#: templates/home.html:11
msgid "god"
msgstr ""

把上段文字的msgstr修改:

#: templates/home.html:9
msgid "wis"
msgstr "维斯"

#: templates/home.html:11
msgid "god"
msgstr "工单"

检测po文件语法

django-admin makemessages -a

生成mo文件

django-admin compilemessages

然后就可以启动django开始访问这个页面了

测试与验证

本文用的httpie来测试的,httpie这里推荐一下,比curl好用些,python写的。

mojideMacBook-Pro-2:~ hongzhi.wang$ http http://127.0.0.1:8000/ Content-Language:zh
HTTP/1.0 200 OK
Content-Language: en
Content-Length: 155
Content-Type: text/html; charset=utf-8
Date: Wed, 26 Jul 2017 03:12:31 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Vary: Accept-Language, Cookie
X-Frame-Options: SAMEORIGIN

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
wis
<h1>hello</h1>
<h1>god</h1>
</body>
</html>

mojideMacBook-Pro-2:~ hongzhi.wang$ http http://127.0.0.1:8000/ Accept-Language:zh
HTTP/1.0 200 OK
Content-Language: zh-hans
Content-Length: 161
Content-Type: text/html; charset=utf-8
Date: Wed, 26 Jul 2017 03:12:48 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Vary: Accept-Language, Cookie
X-Frame-Options: SAMEORIGIN

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
维斯
<h1>hello</h1>
<h1>工单</h1>
</body>
</html>