用户格式到ugettex

2024-09-29 21:41:03 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用foramt到UGETEXT?在

# is not valid code
from django.utils.translation import ugettext as _

def index(req):
    return _('Hello world {}').format('Users')

重复使用矿石:

^{pr2}$

Tags: djangofromimportindexisdefasnot
1条回答
网友
1楼 · 发布于 2024-09-29 21:41:03

第一个将把“'helloworld{}'”标记为可翻译字符串,并将format('Users')应用于(最终翻译的)字符串。打开你的.po文件,你会得到“Hello world{}”

第二个将首先将format('Users')应用于“Hello world{}”,因此可翻译字符串(您将在.po文件中获得的内容)将是“Hello world Users”—实际上,它与将文本“Hello world Users”字符串传递给ugettext完全相同。在

既然您询问了format()在翻译中的使用,我假设您想要第一个(实际上您使用了一些变量作为参数,而不是文本字符串“Users”)。在

实际上,最佳实践是使用关键字args(即_('Hello world {users}').format(users=somevarhere)),这样翻译器就可以得到一些关于占位符代表什么的提示,并且在字符串中包含多个占位符的情况下,可以根据目标语言对它们重新排序。在

相关问题 更多 >

    热门问题