Fix _() when context isn't found; bug 548713

This commit is contained in:
Wil Clouser 2010-02-26 11:55:10 -08:00
Родитель ef7cac4b8f
Коммит f62f1870b9
2 изменённых файлов: 32 добавлений и 9 удалений

Просмотреть файл

@ -10,19 +10,30 @@ from django.utils.translation import (trans_real as django_trans,
def ugettext(message, context=None):
message = strip_whitespace(message)
new_message = strip_whitespace(message)
if context:
message = _add_context(context, message)
return django_ugettext(message)
new_message = _add_context(context, new_message)
ret = django_ugettext(new_message)
if ret == new_message:
return message
return ret
def ungettext(singular, plural, number, context=None):
singular = strip_whitespace(singular)
plural = strip_whitespace(plural)
new_singular = strip_whitespace(singular)
new_plural = strip_whitespace(plural)
if context:
singular = _add_context(context, singular)
plural = _add_context(context, plural)
return django_nugettext(singular, plural, number)
new_singular = _add_context(context, new_singular)
new_plural = _add_context(context, new_plural)
ret = django_nugettext(new_singular, new_plural, number)
# If the context isn't found, the string is returned as it was sent
if ret == new_singular:
return singular
elif ret == new_plural:
return plural
return ret
def _add_context(context, message):
@ -76,10 +87,10 @@ def activate(locale):
jingo.env.install_gettext_translations(t)
def deactivate_all():
""" Override django's utils.translation.deactivate_all(). Django continues
to cache a catalog even if you call their deactivate_all().
"""
django_trans.deactivate_all()
django_trans._translations = {}

Просмотреть файл

@ -43,6 +43,12 @@ def test_ugettext():
eq_(p_text_2, _(a_text, 'context_two'))
@with_setup(setup, teardown)
def test_ugettext_not_found():
eq_('yo', _('yo'))
eq_('yo', _('yo', 'context'))
@with_setup(setup, teardown)
def test_ungettext():
# No context
@ -66,6 +72,12 @@ def test_ungettext():
eq_(p_plural_2, n_(a_singular, a_plural, 3, 'context_two'))
@with_setup(setup, teardown)
def test_ungettext_not_found():
eq_('yo', n_('yo', 'yos', 1, 'context'))
eq_('yos', n_('yo', 'yos', 3, 'context'))
def test_activate():
l10n.deactivate_all()
l10n.activate('fr')