Skip to main content
 首页 » 编程设计

python之特定语言环境的gettext

2025年05月04日17tuyile006

我可以使用当前语言环境获取翻译。

from django.utils.translation import ugettext as _ 
_("password") 

但是在我的代码(具体形式)中,我想获得特定语言的翻译。我想说。

ugettext_magic("de", "password") 

我已经将字符串翻译成我需要的语言。

请您参考如下方法:

上下文管理器 django.utils.translation.override在进入时激活一种新语言并在退出时重新激活以前的事件语言

from django.utils import translation 
 
def get_translation_in(language, s): 
    with translation.override(language): 
        return translation.gettext(s) 
 
print(get_translation_in('de', 'text'))