美文网首页Python开发
python(09)实践Django-国际化

python(09)实践Django-国际化

作者: 灼灼2015 | 来源:发表于2017-08-10 11:51 被阅读84次

    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 = '<a href="{% url "devops:pci-update" pk=99991937 %}" class="btn btn-xs m-l-xs btn-info">{% trans "Update" %}</a>'.replace('99991937', cellData);
    var sync_btn = '<a href="{% url "devops:pci-build-update" pk=99991937 %}" class="btn btn-xs m-l-xs btn-info">{% trans "Sync" %}</a>'.replace('99991937', cellData);
    var del_btn = '<a class="btn btn-xs btn-danger m-l-xs btn_pci_delete" data-uid="99991937">{% trans "Delete" %}</a>'.replace('99991937', cellData);
    
    显示 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-国际化

        本文链接:https://www.haomeiwen.com/subject/qdafrxtx.html