lazy gettext support. Fixes #39
This commit is contained in:
parent
5583523d98
commit
36982a243c
|
@ -3,7 +3,7 @@ from __future__ import unicode_literals
|
|||
|
||||
from email.utils import getaddresses, formataddr
|
||||
|
||||
from .compat import (string_types, is_callable, to_bytes, formataddr as compat_formataddr)
|
||||
from .compat import (string_types, is_callable, to_bytes, formataddr as compat_formataddr, to_unicode)
|
||||
from .utils import (SafeMIMEText, SafeMIMEMultipart, sanitize_address,
|
||||
parse_name_and_email, load_email_charsets,
|
||||
encode_header as encode_header_,
|
||||
|
@ -189,6 +189,9 @@ class MessageBuildMixin(object):
|
|||
# TODO: may be remove header here ?
|
||||
return
|
||||
|
||||
if not isinstance(value, string_types):
|
||||
value = to_unicode(value)
|
||||
|
||||
# Prevent header injection
|
||||
if '\n' in value or '\r' in value:
|
||||
raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (value, key))
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
# encoding: utf-8
|
||||
from __future__ import unicode_literals, print_function
|
||||
import gettext
|
||||
from emails import Message
|
||||
from emails.utils import decode_header
|
||||
|
||||
def lazy_string(func, string, **variables):
|
||||
from speaklater import make_lazy_string
|
||||
return make_lazy_string(func, string, **variables)
|
||||
|
||||
|
||||
def test_lazy_translated():
|
||||
# prepare translations
|
||||
T = gettext.GNUTranslations()
|
||||
T._catalog = {'invitation': 'invitaci\xf3n'}
|
||||
_ = T.gettext
|
||||
|
||||
msg = Message(html='...', subject=lazy_string(_, 'invitation'))
|
||||
assert decode_header(msg.as_message()['subject']) == _('invitation')
|
||||
|
||||
msg = Message(html='...', subject='invitaci\xf3n')
|
||||
assert decode_header(msg.as_message()['subject']) == 'invitaci\xf3n'
|
||||
|
||||
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
jinja2
|
||||
mako
|
||||
speaklater
|
||||
pytest
|
||||
pytest-cov
|
||||
|
|
Reference in New Issue