python - Django : get the current locale inside a templatetag

标签 python django localization django-templates

我有一个国际化的 django (1.4) 应用程序。

在这个应用程序中,我有一个模板标签,它需要知道当前的语言是什么。

我想我可以使用django.utils.translation.get_language,但似乎在模板标签内,它返回默认语言而不是当前语言。

我通过从上下文中读取 request.LANGUAGE_CODE 修复了这个问题。

但是我想知道是否有更好的方法来获取模板标签中的语言

最佳答案

有一些default templatetags你可以使用

{% get_current_language %}

{% get_current_language as FOO %}

例如

{% render_bar request FOO %}

关于python - Django : get the current locale inside a templatetag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16212589/

相关文章:

flutter - easy_localization 对其他语言的键有回退值

.net - 如何设置和更改 WPF 中的文化

Python 一行

python - 使用查找器的正则表达式

python - on_delete = models.PROTECT和on_delete = models.CASCADE对Django模型有什么作用?

python - Elasticsearch 条件

java - 如何在不使用cookie的情况下在spring Controller 中获取客户端时区偏移量?

python - 使用 pyodbc 将日期时间插入到 MS SQL 表中

python - py2exe:MKL fatal error :无法加载 mkl_intel_thread.dll

python - 从基于类的通用 View 手动获取响应