我什么时候应该使用ugettext_lazy? [英] When should I use ugettext_lazy?

查看:1081
本文介绍了我什么时候应该使用ugettext_lazy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于使用ugettext和ugettext_lazy进行翻译的问题。
我了解到,在模型中,我应该使用ugettext_lazy,而在视图ugettext。
但是还有其他的地方,我也应该使用ugettext_lazy吗?表单定义怎么样?
他们之间是否有任何表现差异?



编辑:
还有一件事。有时,而不是 ugettext_lazy ,使用 ugettext_noop 。正如文档所说, ugettext_noop 字符串只能在最新的可能的标记之前被标记为翻译,然后再将它们显示给用户,但我在这里并不困惑类似于 ugettext_lazy 做什么?

解决方案

ugettext () ugettext_lazy()



在像表单或模型这样的定义中使用 ugettext_lazy ,因为这个定义的代码只执行一次(主要是在django的启动时); ugettext_lazy 以懒惰的方式翻译字符串,这意味着例如。每当您访问模型上的属性名称时,字符串将被新翻译 - 这是完全有意义的,因为您可能正在使用不同语言查看此模型,因为django已启动!



在视图和类似的函数调用中,您可以使用 ugettext 而没有任何问题,因为每次视图都被称为 ugettext 将新执行,所以您将始终得到正确的翻译符合要求!



关于 ugettext_noop() / h2>

由于 Bryce 在答案中指出,此功能标记为字符串可解析为翻译,但返回未翻译的字符串。这对于在两个地方使用字符串非常有用 - 翻译和非翻译。请参阅以下示例:

 导入日志
从django.http导入HttpResponse
从django.utils。翻译import ugettext as _,ugettext_noop as _noop

def view(request):
msg = _noop(发生错误)
logging.error(msg)
return HttpResponse(_(msg))


I have a question about using ugettext and ugettext_lazy for translations. I learned that in models I should use ugettext_lazy, while in views ugettext. But are there any other places, where I should use ugettext_lazy too? What about form definitions? Are there any performance diffrences between them?

Edit: And one more thing. Sometimes, instead of ugettext_lazy, ugettext_noop is used. As documentation says, ugettext_noop strings are only marked for translation and translated at the latest possible momment before displaying them to the user, but I'm little confused here, isn't that similar to what ugettext_lazy do? It's still hard for me to decide, which should I use in my models and forms.

解决方案

ugettext() vs. ugettext_lazy()

In definitions like forms or models you should use ugettext_lazy because the code of this definitions is only executed once (mostly on django's startup); ugettext_lazy translates the strings in a lazy fashion, which means, eg. every time you access the name of an attribute on a model the string will be newly translated-which makes totally sense because you might be looking at this model in different languages since django was started!

In views and similar function calls you can use ugettext without problems, because everytime the view is called ugettext will be newly executed, so you will always get the right translation fitting the request!

Regarding ugettext_noop()

As Bryce pointed out in his answer, this function marks a string as extractable for translation but does return the untranslated string. This is useful for using the string in two places – translated and untranslated. See the following example:

import logging
from django.http import HttpResponse
from django.utils.translation import ugettext as _, ugettext_noop as _noop

def view(request):
    msg = _noop("An error has occurred")
    logging.error(msg)
    return HttpResponse(_(msg))

这篇关于我什么时候应该使用ugettext_lazy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆