From 64c0bf86775c5706355c9d3870345b8cff2c63f4 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 17 Nov 2007 12:11:54 +0000 Subject: [PATCH] Translating safe strings should return a safe result. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6681 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/translation/trans_null.py | 6 +++++- django/utils/translation/trans_real.py | 15 ++++++++++----- tests/regressiontests/i18n/tests.py | 14 +++++++++++++- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/django/utils/translation/trans_null.py b/django/utils/translation/trans_null.py index 0b62907e44..98c6de6197 100644 --- a/django/utils/translation/trans_null.py +++ b/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 @@ TECHNICAL_ID_MAP = { } 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)) diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index 98b4769031..c95c842a4f 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -8,6 +8,7 @@ import gettext as gettext_module 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') diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index 0a18e8bea5..2ffc62f90d 100644 --- a/tests/regressiontests/i18n/tests.py +++ b/tests/regressiontests/i18n/tests.py @@ -4,7 +4,7 @@ import misc 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 @@ unicode(string_concat(...)) should not raise a TypeError - #4796 >>> 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) + +>>> activate('de') +>>> type(ugettext(s)) + +>>> deactivate() """ __test__ = {