将包含变量的延迟转换字符串传递给Django中的函数

时间:2012-03-07 18:25:55

标签: django internationalization translation lazy-evaluation

在Django视图中,我创建了一个类似的主题:

subject = _(u"%(user)s has posted a comment") % { 'user': user }

然后我将此主题传递给处理电子邮件通知的函数:

send_notifications(request, subject, url)

在send_notifications中,我遍历所有订阅并发送电子邮件。但是,每个用户可以使用不同的语言,因此我通过Django的激活动态激活用户的语言:

def send_notifications(request, subject, url):
    from django.utils.translation import activate
    for s in Subscription.objects.filter(url=url):
        activate(s.user.userprofile.lang)
        send_mail(subject, render_to_string('notification_email.txt', locals()), settings.SERVER_EMAIL, [s.user.email])

模板以每个用户的正确语言呈现。但是,主题作为已评估和翻译的字符串传递给send_notifications,因此不会被翻译。

我使用惰性翻译和lambda函数作为参数,但没有成功。任何帮助表示赞赏:)

2 个答案:

答案 0 :(得分:1)

不要传递已翻译的主题,只需将其传递给非翻译者:

subject = '%(user)s has posted a comment'
context = {'user': user}

def send_notifications(request, subject, url, context):
    from django.utils.translation import activate
    for s in Subscription.objects.filter(url=url):
        activate(s.user.userprofile.lang)
        send_mail(_(subject) % context, render_to_string('notification_email.txt', locals()), settings.SERVER_EMAIL, [s.user.email])

如果你不打算对每个用户的内容进行个性化设置,那么你也可以限制渲染的数量,因为这有点令人困惑:

# also do your imports at the top to catch import issues early
from django.utils.translation import activate
from django.utils.translation import ugettext as _

def send_notifications(request, url, 
    translatable_subject, context,
    body_template='notification_template.txt'):
    previous_lang = None
    for s in Subscription.objects.filter(url=url).order_by('user__userprofile__lang'):
        if s.user.userprofile.lang != previous_lang:
            activate(s.user.userprofile.lang)
            subject = _(translatable_subject) % context
            body = render_to_string(body_template, locals())
        send_mail(subject, body, settings.SERVER_EMAIL, [s.user.email])
        previous_lang = s.user.userprofile.lang

因此,更明显的是,您不会按使用情况呈现电子邮件。

这种轻微的重写会让你怀疑几个名字的原始选择(本地人,notification_template)。

以上示例代码几乎不是“有根据的猜测”,您应该仔细检查它并确保在粘贴之前了解所有内容。

答案 1 :(得分:1)

好的,我自己找到了解决方案。如果有人遇到类似的问题:

from django.utils.translation import ugettext as _

# create subject as raw string in Django view
raw_subject = r"%(username)s has posted a comment"

# for the sake of generic variables, create a dictionary to pass to function
extra_context = { 'user': user }

# call function with raw string and dictionary as params
send_notifications(request, raw_subject, url, extra_context)

# translate the raw string inside send_notifications into the temporarily activated language
translated_subject = _(raw_subject) % extra_context

似乎正常工作:)由于我们正在处理几种不同的通知,我试图避免为每种类型添加额外的模板。但是,使用extra_context调用模板也是一种可能的解决方案。