$ ls ~yifei/notes/

django 国际化

Posted on:

Last modified:

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-hans", ("中文简体")),
    ("zh-hant", ("中文繁體")),
)
 
#翻译文件所在目录,需要手工创建
LOCALE_PATHS = (
    os.path.join(BASE_DIR, "locale"),
)
 
TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    "django.core.context_processors.i18n",
)

生成需要翻译的文件:

python manage.py makemessages -l zh_hans
python manage.py makemessages -l zh_hant

翻译其中的 django.po 文件,注意.po文件是一种通用的格式,有很多专门的编辑器

编译翻译好的文件

python manage.py compilemessages

© 2016-2022 Yifei Kong. Powered by ynotes

All contents are under the CC-BY-NC-SA license, if not otherwise specified.

Opinions expressed here are solely my own and do not express the views or opinions of my employer.