From b818a41701de952b86d127fa8c1b66eed0a3e4de Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Wed, 28 May 2008 22:35:50 +0000 Subject: [PATCH] changes to make notifications translatable git-svn-id: http://django-notification.googlecode.com/svn/trunk@47 590c3fc9-4838-0410-bb95-17a0c9b37ca9 --- notification/models.py | 12 ++++++++---- notification/templatetags/__init__.py | 0 notification/templatetags/i18n_helper.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 notification/templatetags/__init__.py create mode 100644 notification/templatetags/i18n_helper.py diff --git a/notification/models.py b/notification/models.py index fbc540c3..0aede050 100644 --- a/notification/models.py +++ b/notification/models.py @@ -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: @@ -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): @@ -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): @@ -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" % ( @@ -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), diff --git a/notification/templatetags/__init__.py b/notification/templatetags/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/notification/templatetags/i18n_helper.py b/notification/templatetags/i18n_helper.py new file mode 100644 index 00000000..aaf464f0 --- /dev/null +++ b/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)