gpt4 book ai didi

python - 访问 Django 的 ugettext_lazy 的未翻译内容

转载 作者:太空狗 更新时间:2023-10-29 17:32:12 25 4
gpt4 key购买 nike

我正在寻找一种合理的方法来获取 ugettext_lazyied 字符串的未翻译内容。我找到了两种方法,但我对其中任何一种都不满意:

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_lazyied 字符串的定义和访问它的代码是相差甚远的。

最佳答案

这是你的第二个解决方案的更好版本

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com