Django:用于定义选择字段的元组列表中引导下划线的含义? [英] Django: Meaning of leading underscore in list of tuples used to define choice fields?

查看:130
本文介绍了Django:用于定义选择字段的元组列表中引导下划线的含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到一些例子来定义选择字段:

  COUNTRIES =(
('fr ',_('法国'),
('de',_('德国')),
...

(来源: http:/ / p $ p>

/code.djangoproject.com/ticket/5446
另请参阅: http:// djangosnippets.org/snippets/494/



领先的下划线是什么意思?为什么元组中的第二个值甚至是括号?

解决方案

领先的下划线是常用的函数别名国际化(i18n)机制使用的ugettext功能。



这意味着当您运行i18n时,如果有翻译可用,则选择字段标签将被转换为相应的最终用户语言。



在具有这种语法的文件的顶部,您应该会看到(或者不是,你应该有),如:



from django.utils.translation import ugettext_lazy as _



请参阅 docs here 了解更多详情


I've seen a few examples defining choice fields like so:

COUNTRIES = (
    ('fr', _('France')),
    ('de', _('Germany')),
    ...
)

(source: http://code.djangoproject.com/ticket/5446 Also see: http://djangosnippets.org/snippets/494/)

What is the meaning of the leading underscores? And why is the second value in the tuple even parenthesized?

解决方案

The leading underscore is the commonly used function alias for the one of the ugettext functions used by the internationalization (i18n) mechanics.

It means that when you have i18n running, the choicefield labels will be translated into the appropriate end-user language, if a translation is available.

At the top of a file that features this kind of syntax, you should see (or if not, you should have) something like:

from django.utils.translation import ugettext_lazy as _

See the docs here for more details

这篇关于Django:用于定义选择字段的元组列表中引导下划线的含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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