Meaning of leading underscore in list of tuples used to define choice fields?(用于定义选择字段的元组列表中前导下划线的含义?)
问题描述
我见过一些这样定义选择字段的示例:
I've seen a few examples defining choice fields like so:
COUNTRIES = (
('fr', _('France')),
('de', _('Germany')),
...
)
(来源:http://code.djangoproject.com/ticket/5446另请参阅:http://djangosnippets.org/snippets/494/)
前导下划线是什么意思?为什么元组中的第二个值还要加上括号?
What is the meaning of the leading underscores? And why is the second value in the tuple even parenthesized?
推荐答案
前导下划线是国际化 (i18n) 机制使用的 ugettext 函数之一的常用函数别名.
The leading underscore is the commonly used function alias for the one of the ugettext functions used by the internationalization (i18n) mechanics.
这意味着当您运行 i18n 时,选择字段标签将被翻译成适当的最终用户语言(如果有翻译可用).
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 _
查看此处的文档了解更多详情
这篇关于用于定义选择字段的元组列表中前导下划线的含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用于定义选择字段的元组列表中前导下划线的含义?
基础教程推荐
- 在 Python 中将货币解析为数字 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
