【问题标题】:Django Translation on Heroku is not fully workingHeroku 上的 Django 翻译没有完全工作
【发布时间】:2017-05-02 16:46:38
【问题描述】:

我在 Heroku 上部署了我的 Django 应用程序,并添加了 https://github.com/piotras/heroku-buildpack-gettext.git 这个 ugettext buildpack 并且应用程序的某些部分已翻译注释。在本地它正在工作,我不知道问题来自哪里。

例如,菜单项Classes Types不翻译,但其他部分翻译。

{% trans "Class Types" %}

模板

{% load i18n %}
....
<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">{% trans "Class Types" %}<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li>
                            <a href="{% url 'class_type-list' %}">
                                <span class="glyphicon glyphicon-list" aria-hidden="true"></span> {% trans "All" %}</a>
                        </li>
                        <li>
                            <a href="{% url 'class_type-new' %}">
                                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> {% trans "New" %}</a>
                        </li>
                    </ul>
                </li>
....

ar/LC_MESSAGES.po

#: templates/back_office/class_type_list.html:4
#: templates/back_office/menu.html:37
msgid "Class Types"
msgstr "انواع الحلقات"

zh/LC_MESSAGES.po

#: templates/back_office/class_type_list.html:4
#: templates/back_office/menu.html:37
msgid "Class Types"
msgstr "Class Types"

【问题讨论】:

    标签: python django heroku gettext


    【解决方案1】:

    您可以在构建期间生成这些文件。

    bin/post_compile(没有像procfile这样的扩展名)中添加一个后编译文件,其中包含以下行:

    python manage.py compilemessages
    

    您可以选择添加特定语言(例如python manage.py compilemessages -l nl

    Since Heroku 20,默认 Python 构建包会自动添加 gettext。这仅在构建期间而不是在运行时。因此,该命令应添加到post_compile 中,而不是procfile 中的release: 下。

    旧信息链接到 Heroku 上 gettext 的 buildpacks(例如在上面的问题中)。此配置不再需要这些。

    【讨论】:

      【解决方案2】:

      经过一番研究,我发现最好将编译好的消息文件(*.mo)提交到Heroku,这样翻译就可以正常工作了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-15
        • 2020-04-18
        • 1970-01-01
        • 2010-12-22
        • 1970-01-01
        • 1970-01-01
        • 2014-06-18
        相关资源
        最近更新 更多