如何在Django模板中设置html lang属性?

发布于 2025-01-15 02:09:47 字数 356 浏览 2 评论 0原文

我编写了一个 Django 应用程序,为德语用户提供翻译内容,为其他用户提供英语内容。目前我使用的基本模板包含:

<html lang="en">

显然,如果内容以德语形式提供,这会设置错误的lang

也许我可以尝试类似的方法

<html lang="{{ 'de' if request.LANGUAGE_CODE == 'de' else 'en' }}">

,但这感觉很笨拙,并且在添加更多语言的情况下难以维护。

设置为实际提供的语言的简单方法是什么?

I wrote a Django application that serves translated content for German users, and English content for everyone else. Currently I use a base template containing:

<html lang="en">

Obviously this sets the wrong lang in case the content is served as German.

Possibly I could try something like

<html lang="{{ 'de' if request.LANGUAGE_CODE == 'de' else 'en' }}">

but this feels clumsy and hard to maintain in case more languages are added.

What would be a simple way to set <html lang> to the actual language served?

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

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

发布评论

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

评论(2

待天淡蓝洁白时 2025-01-22 02:09:47

我想通了。

get_current_language 标记包含首选语言用户的。

{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
<html lang="{{ LANGUAGE_CODE }}">

对我来说,文档并没有立即明确表示“首选语言”不是“Accept-Language”HTTP 标头中最顶层的语言,而是始终是最符合用户偏好的 setting.LANGUAGES 语言。

就我而言,settings.py 包含:

LANGUAGE_CODE = "en-us"
LANGUAGES = [("de", _("German")), ("en", _("English"))]

如果用户更喜欢 Italien,则 get_current_language 仍将是 en。对于在端口 8000 上本地运行的 Django 应用程序,您可以尝试以下操作:

curl -H 'Accept-Language: it' http://127.0.0.1:8000/ | cut -b 1-50

输出类似于:

<html lang="en">...

如果请求中没有提供首选语言,则结果是相同的。因此不需要 |default:'en'。您可以使用以下方法进行测试:

curl http://127.0.0.1:8000/ | cut -b 1-50

I figured it out.

The get_current_language tag contains the preferred language of the user.

{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
<html lang="{{ LANGUAGE_CODE }}">

To me the documentation did not make it clear immediately that "preferred language" is not the topmost language from the "Accept-Language" HTTP header but always the language from setting.LANGUAGES that best matches the user's preference.

In my case settings.py contains:

LANGUAGE_CODE = "en-us"
LANGUAGES = [("de", _("German")), ("en", _("English"))]

If the user however prefers for example Italien, get_current_language will still be en. With the Django application running locally on port 8000, you can try this with for example:

curl -H 'Accept-Language: it' http://127.0.0.1:8000/ | cut -b 1-50

The output is something like:

<html lang="en">...

If no preferred language is provided with the request, the result is the same. So there is no need for a |default:'en'. You can test this with:

curl http://127.0.0.1:8000/ | cut -b 1-50
几度春秋 2025-01-22 02:09:47

您可以尝试此操作,

<html lang="{{ request.LANGUAGE_CODE|default:'en' %}">

这将添加 request.LANGUAGE_CODE(如果可用),否则会将默认值设置为 en

You can try this

<html lang="{{ request.LANGUAGE_CODE|default:'en' %}">

this will add request.LANGUAGE_CODE if it available or it will set default value as en

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