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?
我有一个关于使用ugettext和ugettext_lazy进行翻译的问题。我了解到,在模型中,我应该使用ugettext_lazy,而在视图ugettext中。但是有没有其他地方,我也应该使用ugettext_lazy ?形式定义呢?它们之间是否存在性能差异?
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_lazy,而是使用ugettext_noop。如文档所述,ugettext_noop字符串在显示给用户之前只被标记为翻译和翻译,但我在这里有点困惑,这与ugettext_lazy所做的不是很相似吗?我仍然很难决定,我应该在我的模型和表格中使用哪一个。
3 个解决方案
#1
152
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!
在表单或模型等定义中,您应该使用ugettext_lazy,因为此定义的代码只执行一次(主要是在django的启动上);ugettext_lazy以一种懒惰的方式翻译字符串,这意味着,如。每次访问模型上属性的名称时,字符串都会被重新翻译——这是完全有意义的,因为自从django启动以来,您可能正在以不同的语言查看这个模型!
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!
在视图和类似的函数调用中,您可以毫无问题地使用ugettext,因为每次调用ugettext的视图都将被重新执行,因此您将始终获得适合请求的正确翻译!
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:
正如Bryce在回答中指出的,此函数将字符串标记为可提取的转换字符串,但返回未翻译的字符串。这对于在两个地方使用字符串非常有用——翻译的和未翻译的。看下面的例子:
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))
#2
14
An excellent use of _noop, is when you want to log a message in English for the developers, but present the translated string to a viewer. An example of this is at http://blog.bessas.me/posts/using-gettext-in-django/
_noop的一个很好的用法是,当您希望为开发人员用英语记录一条消息时,但是将翻译后的字符串呈现给查看器。一个例子是http://blog.bess.me/posts/using-gettext in django/
#3
3
The lazy version returns a proxy object instead of a string and in some situation it would not work as expected. For example:
惰性版本返回的是代理对象而不是字符串,在某些情况下,它不会像预期的那样工作。例如:
def get(self, request, format=None):
search_str = request.GET.get('search', '')
data = self.search(search_str)
lst = []
lst.append({'name': ugettext_lazy('Client'), 'result': data})
return HttpResponse(json.dumps(lst), content_type='application/json')
would fail because very last line would try serialize lst object into JSON and instead of a string for "client" it would have a proxy object. The proxy object is not serializeable into json.
会失败,因为最后一行会尝试将lst对象序列化为JSON,而不是“client”的字符串,而是一个代理对象。代理对象不能序列化为json。
#1
152
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!
在表单或模型等定义中,您应该使用ugettext_lazy,因为此定义的代码只执行一次(主要是在django的启动上);ugettext_lazy以一种懒惰的方式翻译字符串,这意味着,如。每次访问模型上属性的名称时,字符串都会被重新翻译——这是完全有意义的,因为自从django启动以来,您可能正在以不同的语言查看这个模型!
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!
在视图和类似的函数调用中,您可以毫无问题地使用ugettext,因为每次调用ugettext的视图都将被重新执行,因此您将始终获得适合请求的正确翻译!
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:
正如Bryce在回答中指出的,此函数将字符串标记为可提取的转换字符串,但返回未翻译的字符串。这对于在两个地方使用字符串非常有用——翻译的和未翻译的。看下面的例子:
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))
#2
14
An excellent use of _noop, is when you want to log a message in English for the developers, but present the translated string to a viewer. An example of this is at http://blog.bessas.me/posts/using-gettext-in-django/
_noop的一个很好的用法是,当您希望为开发人员用英语记录一条消息时,但是将翻译后的字符串呈现给查看器。一个例子是http://blog.bess.me/posts/using-gettext in django/
#3
3
The lazy version returns a proxy object instead of a string and in some situation it would not work as expected. For example:
惰性版本返回的是代理对象而不是字符串,在某些情况下,它不会像预期的那样工作。例如:
def get(self, request, format=None):
search_str = request.GET.get('search', '')
data = self.search(search_str)
lst = []
lst.append({'name': ugettext_lazy('Client'), 'result': data})
return HttpResponse(json.dumps(lst), content_type='application/json')
would fail because very last line would try serialize lst object into JSON and instead of a string for "client" it would have a proxy object. The proxy object is not serializeable into json.
会失败,因为最后一行会尝试将lst对象序列化为JSON,而不是“client”的字符串,而是一个代理对象。代理对象不能序列化为json。