2011-05-16 82 views
2

我想將HttpResponseRedirect()中的消息翻譯成german.I在我的網站中有一個彈出框,它顯示信息。信息由視圖中聲明的HttpResonseRedirect給出。我把這種information..the代碼是這樣::Django views-如何翻譯HttpResponseRedirect中的信息消息

如果frmChangePassword.is_valid(): 嘗試:
密碼1 = frmChangePassword.cleaned_data [ '密碼1'] 密碼2 = frmChangePassword.cleaned_data ['密碼2 ']

   if password1 == password2: 
        oe = OEConnector() 
        UpdatePasswordData= oe.updateData('res.users',[request.session['user_id']],{'password':password1}) 
        if UpdatePasswordData: 
         return HttpResponseRedirect('/Info?info=Password changed successfully.') 
        else: 
         return HttpResponseRedirect('/Info?info=Unable to update password. The following error occured -> ' + e.__str__()) 
       else: 
        return HttpResponseRedirect('/Info?info=Passwords you provided did not match.') 

      except Exception as e: 
       return HttpResponseRedirect('/Info?info=Unable to update password. The following error occured -> ' + e.__str__()) 
    elif request.POST['action'] == "newsletter_update": 
     #Check if newsletter_active checkbox has been checked 
     #If not, then seeking this value will throw an error 
     try: 
      request.POST['newsletter_active'] 
     except: 
      newsletter_status = False 
     else: 
      newsletter_status = True 
     oe = OEConnector() 
     UpdateNewsletterData= oe.updateData('res.partner.address',[request.session['partner_id']],{'newsletter_subs':newsletter_status}) 
     return HttpResponseRedirect('/Info?info=Newsletter updated successfully.') . 

如何翻譯在HttpResponseRedirect ......我知道有關於gettext的,反,blocktrans,inlinetrans的消息。 由於事先

回答

0

我想你走錯了路,試圖通過傳遞你的消息通過這樣的一個GET參數。使用django messages framework

from django.contrib import messages 
if password1 == password2: 
    oe = OEConnector() 
    UpdatePasswordData= oe.updateData('res.users',[request.session['user_id']],{'password':password1}) 
    if UpdatePasswordData: 
     messages.success(_('Password changed successfully.')) 
    else: 
     messages.error(_('Unable to update password. The following error occured -> ') + e.__str__()) 
else: 
    messages.error(_('Passwords you provided did not match.')) 
return HttpResponseRedirect('/Info') 

然後你就可以打印模板給錯誤和成功消息不同的CSS樣式的消息:

{% if messages %} 
<ul class="messages"> 
    {% for message in messages %} 
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li> 
    {% endfor %} 
</ul> 
{% endif %} 

嘗試的消息框架,它將使嘗試編碼次數更容易。當然,翻譯這些信息是沒有問題的。

+0

你說:「當然,沒有問題翻譯的消息」。我無法翻譯我的消息。你介意解釋嗎?我添加了'messages.success(請求,「簽上的成功」)'在我看來,和'{%blocktrans%} {{消息}} {%endblocktrans%}我改變之後將無法正常工作.po文件。我認爲Django無法翻譯變量... – user1011444 2013-03-13 16:41:20