django国际化
django国际化
上官清偌 发表于3个月前
django国际化
  • 发表于 3个月前
  • 阅读 36
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: django国际化

最近在研究django国际化,最终还是研究好了

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

MIDDLEWARE_CLASSES = (
   'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',//放在 django.contrib.sessions的后面 
)
 
 
LANGUAGE_CODE = 'zh_Cn'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
 
LANGUAGES = (
    ('en', ('English')),
    ('zh-cn', ('中文简体')),
)
 
#翻译文件所在目录,需要手工创建
LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)
 
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            #insert your TEMPLATE_DIRS here
            os.path.join(BASE_DIR,  'templates'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                "django.core.context_processors.i18n",//记得要加上这句话
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',

                # 'utils.context_processors.clusters',
            ],
        },
    },
]

2:在项目根目录下面创建一个locale文件夹,然后使用命令创建国际化文件:

django-admin.py makemessages -l zh_CN

3:执行完后,locale文件夹下面创建zh_CN/LC_MESSAGES/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-09-11 16:40+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"

#: api/keystone.py:53
#, python-format
msgid "%(type)s (%(backend)s backend)"
msgstr ""

#: api/service/resource/pool_base.py:112
msgid "running"
msgstr ""

#: api/service/resource/pool_base.py:112
msgid "OUT"
msgstr ""

#: api/util.py:26
#, python-format
msgid "%s days %s hours ago"
msgstr ""

#: api/util.py:29
#, python-format
msgid "%s minutes ago"
msgstr ""

#: api/util.py:30
#, python-format
msgid "%s seconds ago"
msgstr ""

#: api/util.py:42 api/util.py:57
#, python-format
msgid "%s days %s hours"
msgstr ""

#: api/util.py:45 api/util.py:60
#, python-format
msgid "%s minutes"
msgstr ""

#: api/util.py:46 api/util.py:61
#, python-format
msgid "%s seconds"
msgstr ""

#: api/util.py:78
msgid "Unable to retrieve instance tenant information."
msgstr ""

#: api/util.py:91
msgid "Unable to retrieve instance size information."
msgstr ""

#: api/util.py:106
msgid "Unable to retrieve instance user information."
msgstr ""

#: templates/template2/menu.html:13
#:菜单
msgid "Dashboard"
msgstr "首页"

msgid "Infrastructure"
msgstr "基础设施"

msgid "Cluster"
msgstr "集群"

msgid "Resource"
msgstr "资源"

msgid "App Center"
msgstr "应用中心"

msgid "System"
msgstr "系统"

#:System下的子菜单
msgid "Authentication"
msgstr "认证"

msgid "Account"
msgstr "帐户"

msgid "Task"
msgstr "任务"

msgid "Schedule"
msgstr "计划表"

msgid "Policy"
msgstr "策略"

msgid "Periodic Task"
msgstr "周期性任务"

msgid "Backup & Log"
msgstr "备份和日志"

msgid "Backup"
msgstr "备份"

msgid "Snapshot"
msgstr "快照"

msgid "Log"
msgstr "日志"

msgid "Template"
msgstr "模板"

msgid "CSM"
msgstr "系统管理"

msgid "Setting"
msgstr "设置"

msgid "Upgrade"
msgstr "升级"

msgid "License"
msgstr "许可证"

#: templates/dashboard/index.html:36
# 主页
msgid "Global View"
msgstr "地图"

4:写好了所有的翻译后,再执行:

django-admin.py compilemessages

5:在html中的应用

  • 注意

<title>{% trans 'str'%}</title>

<!DOCTYPE html>
{% load i18n %}//要加翻译的前缀 
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
<form action="/url 'go'/" method="post" class="smart-green">
    <h1>留言信息
        <span>{% trans 'test' %}</span>//要翻译的信息加入其中
    </h1>
    <label>
        <span>姓名 :</span>
        <input id="name" type="text" name="name" value="{% ifequal my_message.name|slice:"3"  "你好吗" %}我不好{% else %}not name{% endifequal %}" class="error" placeholder="请输入您的姓名"/>
        <div class="error-msg"></div>
    </label>

    <label>
        <span>邮箱 :</span>
        <input id="email" type="email" value="{{ my_message.email }}" name="email" placeholder="请输入邮箱地址"/>
        <div class="error-msg"></div>
    </label>

    <label>
        <span>联系地址 :</span>
        <input id="address" type="text" value="{{ my_message.address }}" name="address" placeholder="请输入联系地址"/>
        <div class="error-msg"></div>
    </label>
    <label>
        <span>留言 :</span>
        <textarea id="message" name="message"   placeholder="请输入你的建议">{{ my_message.message }}</textarea>
        <div class="error-msg"></div>
    </label>
    <div class="success-msg"></div>
    <label>
        <span> </span>
        <input type="submit" class="button" value="提交"/>
    </label>
    <input type='hidden' name='csrfmiddlewaretoken' value='SfHkbL4feo1G00sJQtbO7TtLN4c2BUwa' />
    {% csrf_token %}
</form>

</body>
</html>
标签: django国际化
共有 人打赏支持
粉丝 10
博文 78
码字总数 83811
×
上官清偌
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: