繁体   English   中英

Heroku 上的 Django 翻译无法完全正常工作

[英]Django Translation on Heroku is not fully working

我在 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 "انواع الحلقات"

en/LC_MESSAGES.po

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

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

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

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

python manage.py compilemessages

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM