Python 如何在 Django 中获取当前语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3356964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:37:48  来源:igfitidea点击:

How can I get the current language in Django?

pythondjangointernationalization

提问by diegueus9

How can I get the current language in the current thread in a model or in the admin?

如何在模型或管理中的当前线程中获取当前语言?

采纳答案by micha480

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

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

回答by advait

You can read the system's localefor language information.

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

回答by Ignas But?nas

Or you can also get this in your views

或者你也可以在你的观点中得到这个

request.LANGUAGE_CODE

回答by Stefan Magnuson

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.

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

回答by achedeuzot

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

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

When using the django.utils.translation.get_language()function, it's linked to the threadlanguage. Before Django 1.8, it always returned settings.LANGUAGE_CODEwhen 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:

使用该django.utils.translation.get_language()函数时,它与线程语言相关联。在 Django 1.8 之前,它总是settings.LANGUAGE_CODE在禁用翻译时返回 。如果你想手动覆盖线程语言,你可以使用override()oractivate()函数,它的命名不是很明确,但仍然很有用:

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

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

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

我发现通过谷歌找到关于这个主题的这些差异并不是很容易,所以在这里提供进一步的参考。

回答by Sarath Ak

you can use this

你可以用这个

from django.utils import  translation
translation.get_language()