Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 21st, 2012  |  syntax: None  |  size: 4.16 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. diff --git a/django/utils/encoding.py b/django/utils/encoding.py
  2. index 7b80f13..25d4574 100644
  3. --- a/django/utils/encoding.py
  4. +++ b/django/utils/encoding.py
  5. @@ -94,7 +94,7 @@ def force_text(s, encoding='utf-8', strings_only=False, errors='strict'):
  6.      if strings_only and is_protected_type(s):
  7.          return s
  8.      try:
  9. -        if not isinstance(s, six.string_types):
  10. +        if not isinstance(s, (bytes, six.string_types)):
  11.              if hasattr(s, '__unicode__'):
  12.                  s = s.__unicode__()
  13.              else:
  14. diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
  15. index febde40..39bcf2d 100644
  16. --- a/django/utils/translation/__init__.py
  17. +++ b/django/utils/translation/__init__.py
  18. @@ -79,8 +79,8 @@ def pgettext(context, message):
  19.  def npgettext(context, singular, plural, number):
  20.      return _trans.npgettext(context, singular, plural, number)
  21.  
  22. -ngettext_lazy = lazy(ngettext, bytes)
  23. -gettext_lazy = lazy(gettext, bytes)
  24. +ngettext_lazy = lazy(ngettext, str)
  25. +gettext_lazy = lazy(gettext, str)
  26.  ungettext_lazy = lazy(ungettext, six.text_type)
  27.  ugettext_lazy = lazy(ugettext, six.text_type)
  28.  pgettext_lazy = lazy(pgettext, six.text_type)
  29. diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
  30. index 9e6eadc..3dbbd11 100644
  31. --- a/django/utils/translation/trans_real.py
  32. +++ b/django/utils/translation/trans_real.py
  33. @@ -9,7 +9,7 @@ import gettext as gettext_module
  34.  from threading import local
  35.  
  36.  from django.utils.importlib import import_module
  37. -from django.utils.encoding import smart_str, smart_text
  38. +from django.utils.encoding import force_text, smart_str, smart_text
  39.  from django.utils.safestring import mark_safe, SafeData
  40.  from django.utils import six
  41.  from django.utils.six import StringIO
  42. @@ -261,10 +261,10 @@ def do_translate(message, translation_function):
  43.  def gettext(message):
  44.      return do_translate(message, 'gettext')
  45.  
  46. -if six.PY3:
  47. -    ugettext = gettext
  48. -else:
  49. -    def ugettext(message):
  50. +def ugettext(message):
  51. +    if six.PY3:
  52. +        return do_translate(force_text(message), 'gettext')
  53. +    else:
  54.          return do_translate(message, 'ugettext')
  55.  
  56.  def pgettext(context, message):
  57. @@ -301,14 +301,10 @@ def ngettext(singular, plural, number):
  58.      """
  59.      return do_ntranslate(singular, plural, number, 'ngettext')
  60.  
  61. -if six.PY3:
  62. -    ungettext = ngettext
  63. -else:
  64. -    def ungettext(singular, plural, number):
  65. -        """
  66. -        Returns a unicode strings of the translation of either the singular or
  67. -        plural, based on the number.
  68. -        """
  69. +def ungettext(singular, plural, number):
  70. +    if six.PY3:
  71. +        return do_ntranslate(force_text(singular), force_text(plural), number, 'ngettext')
  72. +    else:
  73.          return do_ntranslate(singular, plural, number, 'ungettext')
  74.  
  75.  def npgettext(context, singular, plural, number):
  76. diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
  77. index 9ca66bd..9332c20 100644
  78. --- a/tests/regressiontests/i18n/tests.py
  79. +++ b/tests/regressiontests/i18n/tests.py
  80. @@ -236,8 +236,18 @@ class TranslationTests(TestCase):
  81.          self.assertEqual(SafeString, type(s))
  82.          with translation.override('de', deactivate=True):
  83.              self.assertEqual(SafeUnicode, type(ugettext(s)))
  84. -        self.assertEqual('aPassword', SafeString('a') + s)
  85. -        self.assertEqual('Passworda', s + SafeString('a'))
  86. +        self.assertEqual(b'aPassword', SafeString(b'a') + s)
  87. +        self.assertEqual(b'Passworda', s + SafeString(b'a'))
  88. +        self.assertEqual(b'Passworda', s + mark_safe(b'a'))
  89. +        self.assertEqual(b'aPassword', mark_safe(b'a') + s)
  90. +        self.assertEqual(b'as', mark_safe(b'a') + mark_safe(b's'))
  91. +        # Same tests for SafeUnicode
  92. +        s = mark_safe('Password')
  93. +        self.assertEqual(SafeUnicode, type(s))
  94. +        with translation.override('de', deactivate=True):
  95. +            self.assertEqual(SafeUnicode, type(ugettext(s)))
  96. +        self.assertEqual('aPassword', SafeUnicode('a') + s)
  97. +        self.assertEqual('Passworda', s + SafeUnicode('a'))
  98.          self.assertEqual('Passworda', s + mark_safe('a'))
  99.          self.assertEqual('aPassword', mark_safe('a') + s)
  100.          self.assertEqual('as', mark_safe('a') + mark_safe('s'))