Ticket #19160: 19160-4.diff
File 19160-4.diff, 6.8 KB (added by , 12 years ago) |
---|
-
django/utils/translation/__init__.py
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index f3cc634..05440e5 100644
a b def npgettext(context, singular, plural, number): 80 80 return _trans.npgettext(context, singular, plural, number) 81 81 82 82 gettext_lazy = lazy(gettext, str) 83 ngettext_lazy = lazy(ngettext, str)84 83 ugettext_lazy = lazy(ugettext, six.text_type) 85 ungettext_lazy = lazy(ungettext, six.text_type)86 84 pgettext_lazy = lazy(pgettext, six.text_type) 87 npgettext_lazy = lazy(npgettext, six.text_type) 85 86 87 def lazy_number(func, resultclasses, number=None, **kwargs): 88 if isinstance(number, int): 89 kwargs['number'] = number 90 proxy = lazy(func, *resultclasses)(**kwargs) 91 else: 92 proxy = lazy(func, *resultclasses)(**kwargs) 93 def mod(self, rhs): 94 if isinstance(rhs, dict) and key: 95 number = rhs[key] 96 else: 97 number = rhs 98 self._proxy____kw['number'] = number 99 result = original_mod(self, rhs) 100 del self._proxy____kw['number'] 101 return result 102 key = number 103 original_mod = getattr(proxy.__class__, '__mod__') 104 setattr(proxy.__class__, '__mod__', mod) 105 return proxy 106 107 def ngettext_lazy(singular, plural, number=None): 108 return lazy_number( 109 ngettext, [str], singular=singular, plural=plural, number=number) 110 111 def ungettext_lazy(singular, plural, number=None): 112 return lazy_number( 113 ungettext, [six.text_type], singular=singular, plural=plural, number=number) 114 115 def npgettext_lazy(context, singular, plural, number=None): 116 return lazy_number( 117 npgettext, [six.text_type], context=context, singular=singular, plural=plural, number=number) 118 119 88 120 89 121 def activate(language): 90 122 return _trans.activate(language) -
docs/topics/i18n/translation.txt
diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt index 65c6fe2..ad7e9d4 100644
a b convert them to strings, because they should be converted as late as possible 408 408 (so that the correct locale is in effect). This necessitates the use of the 409 409 helper function described next. 410 410 411 Lazy translations and plural 412 ---------------------------- 413 414 .. versionadded:: 1.5 415 416 When using lazy translation for a plural string (``[u]n[p]gettext_lazy``), you 417 generally don't know the ``number`` argument at the time of the string 418 definition. Therefore, you are authorized to pass a dictionary key name in place 419 of an integer as the ``number`` argument. Then, when the string is effectively 420 translated with a placeholders dictionary, the ``number`` argument will get 421 substituted by the value of the key in the dictionary. Alternatively, if the 422 string contains only one unnamed placeholder, you can also omit passing the 423 ``number`` argument at all. For example:: 424 425 class MyForm(forms.Form): 426 error1_message = ungettext_lazy("You only provided %(num)d argument", 427 "You only provided %(num)d arguments", 'num') 428 error2_message = ungettext_lazy("You provided %d argument", 429 "You provided %d arguments") 430 431 def clean(self): 432 if err_1: 433 raise forms.ValidationError(self.error1_message % {'num': number}) 434 if err_2: 435 raise forms.ValidationError(self.error2_message % number) 436 437 411 438 Joining strings: string_concat() 412 439 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 413 440 -
tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po
diff --git a/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo b/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo index f825e39..b43f282 100644 Binary files a/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo and b/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo differ diff --git a/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po b/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po index a471d38..e138bb0 100644
a b msgid "May" 35 35 msgstr "Kann" 36 36 37 37 #: models.py:11 38 msgid "%d good result" 39 msgid_plural "%d good results" 40 msgstr[0] "%d gutes Resultat" 41 msgstr[1] "%d guten Resultate" 42 43 #: models.py:11 44 msgid "Hi %(name)s, %(num)d good result" 45 msgid_plural "Hi %(name)s, %(num)d good results" 46 msgstr[0] "Hallo %(name)s, %(num)d gutes Resultat" 47 msgstr[1] "Hallo %(name)s, %(num)d guten Resultate" 48 49 #: models.py:11 38 50 msgctxt "search" 39 51 msgid "%d result" 40 52 msgid_plural "%d results" … … msgstr "Es gibt %(num_comments)s Kommentare" 75 87 #: models.py:23 76 88 msgctxt "other comment count" 77 89 msgid "There are %(num_comments)s comments" 78 msgstr "Andere: Es gibt %(num_comments)s Kommentare" 79 No newline at end of file 90 msgstr "Andere: Es gibt %(num_comments)s Kommentare" -
tests/regressiontests/i18n/tests.py
diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index dcc288e..50654df 100644
a b from django.utils.safestring import mark_safe, SafeBytes, SafeString, SafeText 23 23 from django.utils import six 24 24 from django.utils.six import PY3 25 25 from django.utils.translation import (ugettext, ugettext_lazy, activate, 26 deactivate, gettext_lazy, pgettext, npgettext, to_locale,26 deactivate, gettext_lazy, ungettext_lazy, pgettext, npgettext, to_locale, 27 27 get_language_info, get_language, get_language_from_request, trans_real) 28 28 29 29 … … extended_locale_paths = settings.LOCALE_PATHS + ( 51 51 ) 52 52 53 53 class TranslationTests(TestCase): 54 55 54 def test_override(self): 56 55 activate('de') 57 56 with translation.override('pl'): … … class TranslationTests(TestCase): 95 94 self.assertEqual(six.text_type(s2), "test") 96 95 97 96 @override_settings(LOCALE_PATHS=extended_locale_paths) 97 def test_ungettext_lazy(self): 98 s = ungettext_lazy("%d good result", "%d good result") 99 with translation.override('de'): 100 self.assertEqual(s % 1, "1 gutes Resultat") 101 self.assertEqual(s % 4, "4 guten Resultate") 102 103 s1 = ungettext_lazy("Hi %(name)s, %(num)d good result", "Hi %(name)s, %(num)d good results", 4) 104 s2 = ungettext_lazy("Hi %(name)s, %(num)d good result", "Hi %(name)s, %(num)d good results", 'num') 105 with translation.override('de'): 106 self.assertEqual(s1 % {'num': 4, 'name': 'Jim'}, "Hallo Jim, 4 guten Resultate") 107 self.assertEqual(s2 % {'name': 'Jim', 'num': 1}, "Hallo Jim, 1 gutes Resultat") 108 self.assertEqual(s2 % {'name': 'Jim', 'num': 5}, "Hallo Jim, 5 guten Resultate") 109 110 @override_settings(LOCALE_PATHS=extended_locale_paths) 98 111 def test_pgettext(self): 99 112 trans_real._active = local() 100 113 trans_real._translations = {}