2024-09-29 21:41:03 发布
网友
如何使用foramt到UGETEXT?在
# is not valid code from django.utils.translation import ugettext as _ def index(req): return _('Hello world {}').format('Users')
重复使用矿石:
第一个将把“'helloworld{}'”标记为可翻译字符串,并将format('Users')应用于(最终翻译的)字符串。打开你的.po文件,你会得到“Hello world{}”
format('Users')
第二个将首先将format('Users')应用于“Hello world{}”,因此可翻译字符串(您将在.po文件中获得的内容)将是“Hello world Users”—实际上,它与将文本“Hello world Users”字符串传递给ugettext完全相同。在
ugettext
既然您询问了format()在翻译中的使用,我假设您想要第一个(实际上您使用了一些变量作为参数,而不是文本字符串“Users”)。在
format()
实际上,最佳实践是使用关键字args(即_('Hello world {users}').format(users=somevarhere)),这样翻译器就可以得到一些关于占位符代表什么的提示,并且在字符串中包含多个占位符的情况下,可以根据目标语言对它们重新排序。在
_('Hello world {users}').format(users=somevarhere)
第一个将把“'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)
),这样翻译器就可以得到一些关于占位符代表什么的提示,并且在字符串中包含多个占位符的情况下,可以根据目标语言对它们重新排序。在相关问题 更多 >
编程相关推荐