Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Translating safe strings should return a safe result.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6681 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 64c0bf86775c5706355c9d3870345b8cff2c63f4 1 parent 0928fa5
@malcolmt malcolmt authored
View
6 django/utils/translation/trans_null.py
@@ -4,6 +4,7 @@
from django.conf import settings
from django.utils.encoding import force_unicode
+from django.utils.safestring import mark_safe, SafeData
def ngettext(singular, plural, number):
if number == 1: return singular
@@ -31,7 +32,10 @@ def ungettext(singular, plural, number):
}
def gettext(message):
- return TECHNICAL_ID_MAP.get(message, message)
+ result = TECHNICAL_ID_MAP.get(message, message)
+ if isinstance(message, SafeData):
+ return mark_safe(result)
+ return result
def ugettext(message):
return force_unicode(gettext(message))
View
15 django/utils/translation/trans_real.py
@@ -8,6 +8,7 @@
from cStringIO import StringIO
from django.utils.encoding import force_unicode
+from django.utils.safestring import mark_safe, SafeData
try:
import threading
@@ -271,11 +272,15 @@ def do_translate(message, translation_function):
global _default, _active
t = _active.get(currentThread(), None)
if t is not None:
- return getattr(t, translation_function)(message)
- if _default is None:
- from django.conf import settings
- _default = translation(settings.LANGUAGE_CODE)
- return getattr(_default, translation_function)(message)
+ result = getattr(t, translation_function)(message)
+ else:
+ if _default is None:
+ from django.conf import settings
+ _default = translation(settings.LANGUAGE_CODE)
+ result = getattr(_default, translation_function)(message)
+ if isinstance(message, SafeData):
+ return mark_safe(result)
+ return result
def gettext(message):
return do_translate(message, 'gettext')
View
14 tests/regressiontests/i18n/tests.py
@@ -4,7 +4,7 @@
regressions = ur"""
Format string interpolation should work with *_lazy objects.
->>> from django.utils.translation import ugettext_lazy, activate, deactivate, gettext_lazy
+>>> from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy
>>> s = ugettext_lazy('Add %(name)s')
>>> d = {'name': 'Ringo'}
>>> s % d
@@ -39,6 +39,18 @@
<module 'django.utils.translation' from ...>
>>> unicode(django.utils.translation.string_concat("dja", "ngo"))
u'django'
+
+Translating a string requiring no auto-escaping shouldn't change the "safe"
+status.
+
+>>> from django.utils.safestring import mark_safe
+>>> s = mark_safe('Password')
+>>> type(s)
+<class 'django.utils.safestring.SafeString'>
+>>> activate('de')
+>>> type(ugettext(s))
+<class 'django.utils.safestring.SafeUnicode'>
+>>> deactivate()
"""
__test__ = {
Please sign in to comment.
Something went wrong with that request. Please try again.