用户格式为ugettext

use*_*ers 0 python django python-3.x

如何使用foramt来ugettext?

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

def index(req):
    return _('Hello world {}').format('Users')
Run Code Online (Sandbox Code Playgroud)

矿石使用(更多重复):

_('Hello world {}'.format('Users')) 
Run Code Online (Sandbox Code Playgroud)

bru*_*ers 5

第一个将"'Hello world {}'"标记为可翻译字符串,并将应用于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)),以便翻译者对占位符代表什么有一些暗示,并且在具有多个占位符的字符串的情况下,可以根据目标语言对它们进行重新排序.