2012-03-07 10 views
2

で機能するための変数を含む遅延翻訳文字列を渡し、私はそのような件名を作成:は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に渡されるため、翻訳されません。

私はパラメータとしてレイジー変換とラムダ関数を使用しましたが、成功しませんでした。すべてのヘルプ感謝:)

答えて

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 

、あなたが、使用ごとに電子メールをレンダリングするつもりはないことをはるかに明白です。

このわずかな書き換えは、いくつかの名前(locals、notification_template)の元の選択については疑問に思うはずです。

上記のサンプルコードはほとんど「知識のある推測」であり、それを再度確認し、すべてを理解してから貼り付けるようにしてください。

+0

ありがとうございます。それは本当に良い答えです - ちょうど投票!しかし、問題は:send_notificationは、サブジェクトで使用されているユーザ変数を知らないため、非常に一般的な通知機能です。だから私はテーマテンプレートの 'ユーザー'オブジェクトにアクセスすることはできません: - P –

+0

ああ、あなたの元の答えに非常にクールな追加!ありがとう!私はその変更についての通知を得ておらず、ちょうど今偶然それに遭遇しました。 –

1

[OK]をクリックします。場合には誰もが同様の問題に実行されます。

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でテンプレートを呼び出すことも可能です。

関連する問題