Django 不翻译我标记的字符串

发布于 2024-10-01 00:33:41 字数 2420 浏览 1 评论 0原文

我一直在尝试让翻译在我的 Django 安装上正常工作,但到目前为止都失败了。

我已经创建了一个基本的测试设置来查找我的错误,但到目前为止还没有运气。

我的一个视图只是打印翻译后的文本(或者应该打印,但它总是显示未翻译的文本):

    text = _('blah')
    return HttpResponse(text)

我使用 python manage.py makemessages -l nl_BE 生成了一个荷兰语文件。 django.po 位于 locale/nl_BE/LC_MESSAGES 中。 我修改了该应用程序,将英语中的“blah”翻译为荷兰语中的“blih”:

#: foo/views.py:9`
msgid "blah"
msgstr "blih"

我使用 python manage.pycompilemessages 生成了一个 django.mo 文件。 django.mo 文件也位于 locale/nl_BE/LC_MESSAGES 中。

我尝试将 settings.py (LANGUAGE_CODE) 中的默认语言更改为“nl-be”,但这并没有什么区别(所以现在是“en-us”)。 USE_I18N 设置为 True。

我有一个设置语言的视图(使用 /i18n/setlang/ url)。

在此视图中,我还打印:

{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_current_language_bidi as LANGUAGE_BIDI %}

这表明:

LANGUAGE_CODE: nl
LANGUAGES: [('ar', u'Arabisch'), ('bg', u'Bulgaars'), ('bn', u'Bengaals'), ('bs', u'Bosnian'), ('ca', u'Catalaans'), ('cs', u'Tjechisch'), ('cy', u'Welsh'), ('da', u'Deens'), ('de', u'Duits'), ('el', u'Grieks'), ('en', u'Engels'), ('en-gb', u'British English'), ('es', u'Spaans'), ('es-ar', u'Argentinian Spanish'), ('et', u'Ests'), ('eu', u'Baskisch'), ('fa', u'Perzisch'), ('fi', u'Fins'), ('fr', u'Frans'), ('fy-nl', u'Frisian'), ('ga', u'Iers'), ('gl', u'Galicisch'), ('he', u'Hebreews'), ('hi', u'Hindi'), ('hr', u'Kroatisch'), ('hu', u'Hongaars'), ('id', u'Indonesian'), ('is', u'IJslands'), ('it', u'Italiaans'), ('ja', u'Japans'), ('ka', u'Georgisch'), ('km', u'Khmer'), ('kn', u'Kannada'), ('ko', u'Koreaans'), ('lt', u'Litouws'), ('lv', u'Lets'), ('mk', u'Macedonisch'), ('ml', u'Malayalam'), ('mn', u'Mongolian'), ('nl', u'Nederlands'), ('no', u'Noors'), ('nb', u'Norwegian Bokmal'), ('nn', u'Norwegian Nynorsk'), ('pl', u'Pools'), ('pt', u'Portugees'), ('pt-br', u'Braziliaans Portugees'), ('ro', u'Roemeens'), ('ru', u'Russisch'), ('sk', u'Slovaaks'), ('sl', u'Sloveens'), ('sq', u'Albanian'), ('sr', u'Servisch'), ('sr-latn', u'Serbian Latin'), ('sv', u'Zweeds'), ('ta', u'Tamil'), ('te', u'Telegu'), ('th', u'Thais'), ('tr', u'Turks'), ('uk', u'Oekra\xefens'), ('vi', u'Vietnamese'), ('zh-cn', u'Vereenvoudigd Chinees'), ('zh-tw', u'Traditioneel Chinees')]
LANGUAGE_BIDI: False

So nl 应该 工作。事实上,确实如此。 LANGUAGES 变量显示以荷兰语显示的语言列表。 但是,它不显示我的应用程序的荷兰语翻译。

Django 不知何故没有注意到我的 django.mo 文件吗? 我是否忘记了一些重要的事情?

I've been trying to get translations working on my Django installation, but have failed so far.

I've created a basic test setup to find my error, but no luck so far.

One of my views simply prints a translated text (or it should, but it always shows the non-translated text):

    text = _('blah')
    return HttpResponse(text)

I generated a Dutch language file using python manage.py makemessages -l nl_BE. The django.po is in locale/nl_BE/LC_MESSAGES.
I modified that application to translate "blah" in English to "blih" in Dutch:

#: foo/views.py:9`
msgid "blah"
msgstr "blih"

I've generated a django.mo file using python manage.py compilemessages. The django.mo file is also in locale/nl_BE/LC_MESSAGES.

I've tried to change the default language in settings.py (LANGUAGE_CODE) to 'nl-be', but that didn't make a difference (so it's now on 'en-us').
USE_I18N is set to True.

I have a view that sets the language (using the /i18n/setlang/ url).

In this view, I also print:

{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_current_language_bidi as LANGUAGE_BIDI %}

This shows:

LANGUAGE_CODE: nl
LANGUAGES: [('ar', u'Arabisch'), ('bg', u'Bulgaars'), ('bn', u'Bengaals'), ('bs', u'Bosnian'), ('ca', u'Catalaans'), ('cs', u'Tjechisch'), ('cy', u'Welsh'), ('da', u'Deens'), ('de', u'Duits'), ('el', u'Grieks'), ('en', u'Engels'), ('en-gb', u'British English'), ('es', u'Spaans'), ('es-ar', u'Argentinian Spanish'), ('et', u'Ests'), ('eu', u'Baskisch'), ('fa', u'Perzisch'), ('fi', u'Fins'), ('fr', u'Frans'), ('fy-nl', u'Frisian'), ('ga', u'Iers'), ('gl', u'Galicisch'), ('he', u'Hebreews'), ('hi', u'Hindi'), ('hr', u'Kroatisch'), ('hu', u'Hongaars'), ('id', u'Indonesian'), ('is', u'IJslands'), ('it', u'Italiaans'), ('ja', u'Japans'), ('ka', u'Georgisch'), ('km', u'Khmer'), ('kn', u'Kannada'), ('ko', u'Koreaans'), ('lt', u'Litouws'), ('lv', u'Lets'), ('mk', u'Macedonisch'), ('ml', u'Malayalam'), ('mn', u'Mongolian'), ('nl', u'Nederlands'), ('no', u'Noors'), ('nb', u'Norwegian Bokmal'), ('nn', u'Norwegian Nynorsk'), ('pl', u'Pools'), ('pt', u'Portugees'), ('pt-br', u'Braziliaans Portugees'), ('ro', u'Roemeens'), ('ru', u'Russisch'), ('sk', u'Slovaaks'), ('sl', u'Sloveens'), ('sq', u'Albanian'), ('sr', u'Servisch'), ('sr-latn', u'Serbian Latin'), ('sv', u'Zweeds'), ('ta', u'Tamil'), ('te', u'Telegu'), ('th', u'Thais'), ('tr', u'Turks'), ('uk', u'Oekra\xefens'), ('vi', u'Vietnamese'), ('zh-cn', u'Vereenvoudigd Chinees'), ('zh-tw', u'Traditioneel Chinees')]
LANGUAGE_BIDI: False

So nl should work. And in fact, it does. The LANGUAGES variable shows a list of languages displayed in Dutch.
However, it doesn't display the Dutch translation for my application.

Is Django somehow not noticing my django.mo file?
Am I forgetting something crucial?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文