Code

Ticket #5056: ugettext.2.diff

File ugettext.2.diff, 5.4 KB (added by Christopher Lenz <cmlenz@…>, 7 years ago)

Updated patch

Line 
1Index: django/utils/translation/trans_real.py
2===================================================================
3--- django/utils/translation/trans_real.py      (revision 123)
4+++ django/utils/translation/trans_real.py      (working copy)
5@@ -247,32 +247,23 @@
6         _default = translation(settings.LANGUAGE_CODE)
7     return _default
8 
9-def do_translate(message, translation_function):
10+def gettext(message):
11     """
12-    Translate 'message' using the given 'translation_function' name -- which
13-    will be either gettext or ugettext.
14+    This function will be patched into the builtins module to provide the _
15+    helper function. It will use the current thread as a discriminator to find
16+    the translation object to use. If no current translation is activated, the
17+    message will be run through the default translation object.
18     """
19     global _default, _active
20     t = _active.get(currentThread(), None)
21     if t is not None:
22-        return getattr(t, translation_function)(message)
23+        return t.ugettext(message)
24     if _default is None:
25         from django.conf import settings
26         _default = translation(settings.LANGUAGE_CODE)
27-    return getattr(_default, translation_function)(message)
28+    return _default.ugettext(message)
29+ugettext = gettext
30 
31-def gettext(message):
32-    """
33-    This function will be patched into the builtins module to provide the _
34-    helper function. It will use the current thread as a discriminator to find
35-    the translation object to use. If no current translation is activated, the
36-    message will be run through the default translation object.
37-    """
38-    return do_translate(message, 'gettext')
39-
40-def ugettext(message):
41-    return do_translate(message, 'ugettext')
42-
43 def gettext_noop(message):
44     """
45     Marks strings for translation but doesn't translate them now. This can be
46@@ -282,31 +273,21 @@
47     """
48     return message
49 
50-def do_ntranslate(singular, plural, number, translation_function):
51+def ngettext(singular, plural, number):
52+    """
53+    Returns a unicode string of the translation of either the singular or
54+    plural, based on the number.
55+    """
56     global _default, _active
57-
58     t = _active.get(currentThread(), None)
59     if t is not None:
60-        return getattr(t, translation_function)(singular, plural, number)
61+        return t.ungetext(singular, plural, number)
62     if _default is None:
63         from django.conf import settings
64         _default = translation(settings.LANGUAGE_CODE)
65-    return getattr(_default, translation_function)(singular, plural, number)
66+    return _default.ungettext(singular, plural, number)
67+ungettext = ngettext
68 
69-def ngettext(singular, plural, number):
70-    """
71-    Returns a UTF-8 bytestring of the translation of either the singular or
72-    plural, based on the number.
73-    """
74-    return do_ntranslate(singular, plural, number, 'ngettext')
75-
76-def ungettext(singular, plural, number):
77-    """
78-    Returns a unicode strings of the translation of either the singular or
79-    plural, based on the number.
80-    """
81-    return do_ntranslate(singular, plural, number, 'ungettext')
82-
83 def check_for_language(lang_code):
84     """
85     Checks whether there is a global language file for the given language
86Index: django/utils/translation/__init__.py
87===================================================================
88--- django/utils/translation/__init__.py        (revision 118)
89+++ django/utils/translation/__init__.py        (working copy)
90@@ -48,28 +48,23 @@
91 
92 def gettext_noop(message):
93     return real_gettext_noop(message)
94-
95 ugettext_noop = gettext_noop
96 
97 def gettext(message):
98     return real_gettext(message)
99+ugettext = gettext
100 
101 def ngettext(singular, plural, number):
102     return real_ngettext(singular, plural, number)
103+ungettext = ngettext
104 
105-def ugettext(message):
106-    return real_ugettext(message)
107-
108-def ungettext(singular, plural, number):
109-    return real_ungettext(singular, plural, number)
110-
111 def string_concat(*strings):
112     return real_string_concat(*strings)
113 
114 ngettext_lazy = lazy(ngettext, str)
115 gettext_lazy = lazy(gettext, str)
116-ungettext_lazy = lazy(ungettext, unicode)
117-ugettext_lazy = lazy(ugettext, unicode)
118+ungettext_lazy = ngettext_lazy
119+ugettext_lazy = gettext_lazy
120 string_concat = lazy(string_concat, unicode)
121 
122 def activate(language):
123Index: django/utils/translation/trans_null.py
124===================================================================
125--- django/utils/translation/trans_null.py      (revision 118)
126+++ django/utils/translation/trans_null.py      (working copy)
127@@ -6,13 +6,9 @@
128 from django.utils.encoding import force_unicode
129 
130 def ngettext(singular, plural, number):
131-    if number == 1: return singular
132-    return plural
133-ngettext_lazy = ngettext
134+    return force_unicode({1: singular}.get(number, plural))
135+ungettext = ngettext_lazy = ngettext
136 
137-def ungettext(singular, plural, number):
138-    return force_unicode(ngettext(singular, plural, number))
139-
140 string_concat = lambda *strings: u''.join([force_unicode(el) for el in strings])
141 activate = lambda x: None
142 deactivate = deactivate_all = install = lambda: None
143@@ -32,13 +28,9 @@
144 }
145 
146 def gettext(message):
147-    return TECHNICAL_ID_MAP.get(message, message)
148+    return force_unicode(TECHNICAL_ID_MAP.get(message, message))
149+ugettext = gettext_noop = gettext_lazy = _ = gettext
150 
151-def ugettext(message):
152-    return force_unicode(gettext(message))
153-
154-gettext_noop = gettext_lazy = _ = gettext
155-
156 def to_locale(language):
157     p = language.find('-')
158     if p >= 0: