Skip to content

Commit

Permalink
changes to make notifications translatable
Browse files Browse the repository at this point in the history
git-svn-id: http://django-notification.googlecode.com/svn/trunk@47 590c3fc9-4838-0410-bb95-17a0c9b37ca9
  • Loading branch information
jezdez committed May 28, 2008
1 parent 5dd5011 commit b818a41
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
12 changes: 8 additions & 4 deletions notification/models.py
Expand Up @@ -11,6 +11,7 @@
from django.contrib.auth.models import User

from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext

# favour django-mailer but fall back to django.core.mail
try:
Expand Down Expand Up @@ -178,6 +179,7 @@ class FormatException(Exception):

def decode_message(message, decoder):
out = []
objects = []
in_field = False
prev = 0
for index, ch in enumerate(message):
Expand All @@ -194,13 +196,15 @@ def decode_message(message, decoder):
raise FormatException("{ inside {}")
elif ch == '}':
in_field = False
out.append(decoder(message[prev+1:index]))
objects.append(decoder(message[prev+1:index]))
out.append("%s")
prev = index + 1
if in_field:
raise FormatException("unmatched {")
if prev <= index:
out.append(message[prev:index+1])
return "".join(out)
result = "".join(out)
return ugettext(result) % tuple(objects)

def message_to_text(message):
def decoder(ref):
Expand All @@ -221,7 +225,7 @@ def send(users, notice_type_label, message_template, object_list=[], issue_notic
This is intended to be how other apps create new notices.
"""
notice_type = NoticeType.objects.get(label=notice_type_label)
message = encode_message(message_template, *object_list)
message = encode_message(ugettext(message_template), *object_list)
recipients = []

notices_url = u"http://%s%s" % (
Expand All @@ -230,7 +234,7 @@ def send(users, notice_type_label, message_template, object_list=[], issue_notic
)

subject = render_to_string("notification/notification_subject.txt", {
"display": notice_type.display,
"display": ugettext(notice_type.display),
})
message_body = render_to_string("notification/notification_body.txt", {
"message": message_to_text(message),
Expand Down
Empty file.
10 changes: 10 additions & 0 deletions notification/templatetags/i18n_helper.py
@@ -0,0 +1,10 @@
from django.template.defaultfilters import stringfilter
from django.utils.translation import ugettext
from django import template

register = template.Library()

def do_ugettext(msg):
"""Given a message this returns its gettext translation"""
return ugettext(msg)
register.simple_tag('ugettext', do_ugettext)

0 comments on commit b818a41

Please sign in to comment.