Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ugettext的用户格式_Python_Django_Python 3.x - Fatal编程技术网

Python ugettext的用户格式

Python ugettext的用户格式,python,django,python-3.x,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') 矿石使用(创建更多副本): 第一个将“Hello world{}”标记为可翻译字符串,并将格式('Users')应用于(最终翻译的)字符串。在你的.po文件中,你会看到“Hello wor

如何使用foramt到ugettext

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

def index(req):
    return _('Hello world {}').format('Users')
矿石使用(创建更多副本):

第一个将“Hello world{}”标记为可翻译字符串,并将
格式('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)
),这样译者就可以对占位符的含义有一些提示,如果字符串有多个占位符,就可以根据目标语言对其重新排序。

第一个将标记为“'Hello world{}”作为可翻译字符串,并将
格式('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)
),这样翻译器就可以对占位符的含义有一些提示,如果字符串中有多个占位符,可以根据目标语言对其重新排序。

这取决于您是否希望翻译字符串
用户
not@Sayse为什么?字符串:“Hello world Users”返回函数。我不确定我之前的评论有什么不清楚的地方,或者你在评论中的意思是什么。这取决于你是否希望字符串
用户
被翻译或删除not@Sayse为什么?字符串:“Hello world Users”返回函数。我不确定我之前的评论有什么不清楚的地方,或者你在评论中的意思
_('Hello world {}'.format('Users'))