如何获取Django当前的语言?

发布于 2024-09-12 13:16:43 字数 26 浏览 6 评论 0 原文

如何获取 Django 中的当前语言?

How can I get the current language in Django?

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

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

发布评论

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

评论(7

花开柳相依 2024-09-19 13:16:43

特别感兴趣的函数是 django.utils.translation.get_language() ,它返回当前线程中使用的语言。请参阅文档

Functions of particular interest are django.utils.translation.get_language() which returns the language used in the current thread. See documentation.

扶醉桌前 2024-09-19 13:16:43

或者您也可以在您的视图中获取此内容

request.LANGUAGE_CODE

Or you can also get this in your views

request.LANGUAGE_CODE
笑着哭最痛 2024-09-19 13:16:43

注意获取语言的方法。根据哪种方法,Django 将使用不同的方式和信息来确定要使用的正确语言。

当使用django.utils.translation.get_language()函数时,它链接到线程语言。在 Django 1.8 之前,当翻译被禁用时,它总是返回 settings.LANGUAGE_CODE。如果你想手动覆盖线程语言,你可以使用override()或activate()函数,它们的名字不是很明确,但是仍然有用:

from django.utils import translation

with translation.override('fr'):
    print(_("Hello")) # <= will be translated inside the with block

translation.activate('fr') # <= will change the language for the whole thread.
# You then have to manually "restore" the language with another activate()
translation.activate('en') # <= change languages manually

如果您希望 django 检查路径和/或请求(语言 cookie,...),这更常见,例如 www.example.com/en/ www.example.com/fr/ 相比,使用 django.utils.translation.get_language_from_request(request, check_path=False)。此外,它总是会返回在 settings.LANGUAGES 中设置的有效语言。

我发现通过 Google 找到有关此主题的这些差异并不容易,因此这里供进一步参考。

Be careful of the method you use to get the language. Depending on which method, Django will use different ways and informations to determine the right language to use.

When using the django.utils.translation.get_language() function, it's linked to the thread language. Before Django 1.8, it always returned settings.LANGUAGE_CODE when translations were disabled. If you want to manually override the thread language, you can use the override() or activate() functions, which is not very explicitly named, but well, still useful:

from django.utils import translation

with translation.override('fr'):
    print(_("Hello")) # <= will be translated inside the with block

translation.activate('fr') # <= will change the language for the whole thread.
# You then have to manually "restore" the language with another activate()
translation.activate('en') # <= change languages manually

If you want django to check the path and/or request (language cookie, ...), which is a lot more common e.g. www.example.com/en/<somepath> vs www.example.com/fr/<somepath>, use django.utils.translation.get_language_from_request(request, check_path=False). Also, it will always return a valid language set in settings.LANGUAGES

I found it not very easy to find these differences through Google about this subject so here it is for further reference.

眼眸里的那抹悲凉 2024-09-19 13:16:43

只是补充一点,如果您确实使用 django.utils.translation.get_language() 那么您应该记住,如果该部分代码将被异步调用(例如作为 celery 任务),那么这种方法由于它在不同的线程中运行,因此无法工作。

Just to add that if you do use django.utils.translation.get_language() then you should bear in mind that if that section of code will be called asynchronously (e.g. as a celery task) then this approach won't work due to it running in a different thread.

过去的过去 2024-09-19 13:16:43

您可以在 Django 模板语言中使用这些模板标签

{% load i18n %}

{% get_current_language as LANGUAGE_CODE %}

Current language code: {{ LANGUAGE_CODE }}<br>

{% get_current_language_bidi as LANGUAGE_BIDI %}

{% if LANGUAGE_BIDI %}RTL <br>{% endif %}

{% get_language_info for LANGUAGE_CODE as lang %}

Language code: {{ lang.code }}<br>
Name of language: {{ lang.name_local }}<br>
Name in English: {{ lang.name }}<br>
Bi-directional: {{ lang.bidi }}
Name in the active language: {{ lang.name_translated }}

You can use these template tags in Django's templating language:

{% load i18n %}

{% get_current_language as LANGUAGE_CODE %}

Current language code: {{ LANGUAGE_CODE }}<br>

{% get_current_language_bidi as LANGUAGE_BIDI %}

{% if LANGUAGE_BIDI %}RTL <br>{% endif %}

{% get_language_info for LANGUAGE_CODE as lang %}

Language code: {{ lang.code }}<br>
Name of language: {{ lang.name_local }}<br>
Name in English: {{ lang.name }}<br>
Bi-directional: {{ lang.bidi }}
Name in the active language: {{ lang.name_translated }}
水染的天色ゝ 2024-09-19 13:16:43

您可以使用 LANGUAGE_CODEget_language() 如下所示:

# "views.py"

from django.shortcuts import render
from django.utils.translation import get_language

def test(request):
    print(request.LANGUAGE_CODE) # en
    print(get_language()) # en
    return render(request, 'index.html')

并且,您可以使用 LANGUAGE_CODEget_current_language 如下所示。 *get_current_language 需要加载 i18n

{% "index.html" %}

{% load i18n %}

{{ request.LANGUAGE_CODE }} {% "en" %}

{% get_current_language as current_language %}
{{ current_language }} {% "en" %}

You can get the current language with LANGUAGE_CODE and get_language() in Django Views as shown below:

# "views.py"

from django.shortcuts import render
from django.utils.translation import get_language

def test(request):
    print(request.LANGUAGE_CODE) # en
    print(get_language()) # en
    return render(request, 'index.html')

And, you can get the current language with LANGUAGE_CODE and get_current_language in Django Templates as shown below. *get_current_language needs to load i18n:

{% "index.html" %}

{% load i18n %}

{{ request.LANGUAGE_CODE }} {% "en" %}

{% get_current_language as current_language %}
{{ current_language }} {% "en" %}
相权↑美人 2024-09-19 13:16:43

您可以阅读系统的 locale 以获取语言信息。

You can read the system's locale for language information.

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