Django i18n 不工作

发布于 2024-12-10 18:19:28 字数 615 浏览 0 评论 0原文

我无法让翻译显示在我的模板中。这是我所做的:

  1. 在我的模板文件 (index.html) 中,我加载了 i18n 并在需要翻译的地方使用了 trans 标记。
  2. 在我的项目目录中,我运行 ./manage.py makemessages -l es
  3. 我的 django.po 文件位于 ./conf/locale/es/LC_MESSAGES/django.po
  4. 我用翻译编辑了这个文件。
  5. 然后我运行 ./manage.pycompilemessages
  6. 我在 settings.py 中将我的 LANGUAGE_CODE 设置为“es”
  7. 添加了 django.middleware.locale.LocaleMiddleware到 SessionMiddleware 和 CommonMiddleware 之间的 MIDDLEWARE_CLASSES
  8. 重新启动 Django,刷新我的页面。

只有英文表示仍在显示。我在这里做错了什么?我错过了什么吗?

I cannot get translations to show up for my templates. Here is what I have done:

  1. In my template file (index.html), I have loaded i18n and used the trans tag where I want translations.
  2. In my project directory, I rand ./manage.py makemessages -l es
  3. My django.po file is in ./conf/locale/es/LC_MESSAGES/django.po
  4. I edited this file with the translations.
  5. I then ran ./manage.py compilemessages
  6. I set my LANGUAGE_CODE to 'es' in settings.py
  7. Added django.middleware.locale.LocaleMiddleware to MIDDLEWARE_CLASSES in between SessionMiddleware and CommonMiddleware.
  8. Restarted Django, refreshed my page.

Only the english representations are still showing. What am I doing wrong here? Did I miss something?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隐诗 2024-12-17 18:19:28

您是否尝试过在设置文件中设置 LOCALE_PATHS ?这为我解决了这个问题...有关更多信息,请查看我为类似问题编写的答案或django的官方文档LOCALE_PATHS 设置

Have you tried setting LOCALE_PATHS in your settings file? That fixed it for me...for more info check out an answer I wrote for a similar question or django's official documentation for the LOCALE_PATHS setting.

私藏温柔 2024-12-17 18:19:28

Django 的 LocaleMiddleware 尝试根据给定请求确定区域设置,并相应地替换 LANGUAGE_CODE。它执行以下步骤:

  1. 检查会话中名为 django_language 的会话变量。在此会话中将语言设置为给定区域设置。如果不存在,则转到 2。
  2. 检查 cookie 中是否有类似的变量集(您可以自己定义名称)。如果它不存在,则转到 3。
  3. 尝试从 HTTP_ACCEPT_LANGUAGE 标头确定语言。如果不可能,它将转到 4。
  4. 最后它将使用 LANGUAGE_CODE 设置作为给定的语言环境。

在除 4 之外的所有步骤中,它将检查您的 LANGUAGES 设置中是否给出了语言,该设置是表示语言代码和语言标题的 2 元组的元组。确保设置/覆盖默认值,因为默认值将是所有可用 Django 语言的元组。

如果您的 Django 站点有多种语言:

请确保您的会话或 cookie 设置正确。您可以使用 Django 的内部视图来设置语言 (django.views.i18n.set_language)。

如果您只有一种语言:

如果您只有西班牙语,您通常不需要为此烦恼。只需确保设置了 LANGUAGESLANGUAGE_CODE 即可。如果 LANGUAGES 中未提供,它不会尝试将页面语言设置为您的浏览器语言。此外,您应该确保 USE_I18N = True。不过,这应该是默认值,因此至少请确保您没有在某处将其设置为 False。

Django's LocaleMiddleware tries to determine the locale from the given request and replaces the LANGUAGE_CODE accordingly. It goes through the following steps:

  1. Checks the session for a session variable called django_language. Set the language to the given locale in this session. If not existing it goes to 2.
  2. Checks the cookies for a similar variable set (you can define the name yourself). It not existing it goes to 3.
  3. Tries to determine the language from the HTTP_ACCEPT_LANGUAGE header. If not possible it will go to 4.
  4. At last it will use the LANGUAGE_CODE setting as given locale.

In all steps except 4 it will check if the language is given in your LANGUAGES setting, which is a tuple of 2-tuples representing language code and language title. Make sure you set/overwrite the default, since the default would be a tuple of all available Django languages.

If you have multiple languages in your Django site:

Make sure your session or cookies are set correctly. You could use Django's internal view for setting the language (django.views.i18n.set_language).

If you got only one language:

If you only have Spanish you usually don't need to bother with this. Just make sure LANGUAGES and LANGUAGE_CODE is set. It will not attempt to set the pages language to your browsers language if it is not provided in LANGUAGES. Additionally you should make sure that USE_I18N = True. That should be the default though, so make at least sure you didn't set it to False somewhere.

盛夏尉蓝 2024-12-17 18:19:28

好的,我发现了问题...我的 po 文件位于 ./conf/locale 中,而不仅仅是 ./locale 中。

我创建 ./conf/locale 的原因是当我运行 django-admin.py makemessages 时,它指示我这样做。但是,如果它位于您的本地项目目录中,那么它只是简单的 ./locale

Ok, I found the problem... My po files were in ./conf/locale instead of just ./locale.

The reason I created ./conf/locale was because when I rand django-admin.py makemessages, it instructed me to do so. However, if it's in your local project directory, then it is just simply ./locale.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文