python(09)实践Django-国际化

Django 支持国际化,多语言,而本项目做国际化完全是因为个人不喜欢在页面上写N多中文的描述。

  1. setting配置
MIDDLEWARE_CLASSES = (
    ...
    'django.middleware.locale.LocaleMiddleware',
)
LANGUAGE_CODE = 'en'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
#翻译文件所在目录,需要手工创建
LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)
template中加
  'django.template.context_processors.i18n',
  1. 生成需要翻译的文件
    python manage.py makemessages -l zh_hans
    python manage.py makemessages -l zh_hant

  2. 编写文件
    locale/zh/LC_MESSAGES/django.po

msgid "Action"
msgstr "动作"

msgid "Execute"
msgstr "执行"

msgid "Update"
msgstr "更新"

msgid "Release"
msgstr "发布"

msgid "Sync"
msgstr "同步"

msgid "build_number"
msgstr "构建号"
  1. 编译
    django-admin.py compilemessages

  2. 页面上使用

var update_btn = '{% trans "Update" %}'.replace('99991937', cellData);
var sync_btn = '{% trans "Sync" %}'.replace('99991937', cellData);
var del_btn = '{% trans "Delete" %}'.replace('99991937', cellData);

显示
python(09)实践Django-国际化_第1张图片
Paste_Image.png
  1. 遇到的错误
    CommandError: Can't find msgfmt.
    Make sure you have GNU gettext tools 0.15 or newer installed.
    解决方案:安装gettext-iconv解决,下载地址:https://mlocati.github.io/articles/gettext-iconv-windows.html

你可能感兴趣的:(python(09)实践Django-国际化)