django:内含变量的翻译 [英] django: Translation with variables inside

查看:54
本文介绍了django:内含变量的翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

from django.utils.translation import ugettext as _
task = _('You have %s friends') %(c1.task)
// This is translation
#: compositions/views.py:69
#, fuzzy, python-format
msgid "You have %s friends"
msgstr "У вас %s друга"

但是由于某些原因,此msgstr无法正常工作...

But for some reason this msgstr does not work...

推荐答案

也许尝试使用字符串占位符-从

Maybe try using string placeholders - from the django documentation:

您传递给_()或ugettext()的字符串可以占位符,用Python的标准命名字符串插值语法指定.示例:

The strings you pass to _() or ugettext() can take placeholders, specified with Python’s standard named-string interpolation syntax. Example:

def my_view(request, m, d):
    output = _('Today is %(month)s %(day)s.') % {'month': m, 'day': d}
    return HttpResponse(output)

将此应用于您的示例,您将得到:

Applying this to your example, you'd get:

task = _('You have %(num_friends)s friends') % {'num_friends': c1.task}

这篇关于django:内含变量的翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆