我正在寻找一种合理的方法来获取 ugettext_lazy
ied 字符串的未翻译内容。我找到了两种方法,但我对其中任何一种都不满意:
the_string = ugettext_lazy('the content')
the_content = the_string._proxy____args[0] # ewww!
或
from django.utils.translation import activate, get_language
from django.utils.encoding import force_unicode
the_string = ugettext_lazy('the content')
current_lang = get_language()
activate('en')
the_content = force_unicode(the_string)
activate(current_lang)
第一段代码访问了一个明确标记为私有(private)的属性,因此不知道这段代码能工作多长时间。第二种解决方案过于冗长且缓慢。
当然,在实际代码中,ugettext_lazy
ied 字符串的定义和访问它的代码是相差甚远的。
最佳答案
这是你的第二个解决方案的更好版本
from django.utils import translation
the_string = ugettext_lazy('the content')
with translation.override('en'):
content = unicode(the_string)
关于python - 访问 Django 的 ugettext_lazy 的未翻译内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5143822/