Ticket #9988: pgettext-2.patch
File pgettext-2.patch, 11.7 KB (added by , 14 years ago) |
---|
-
django/core/management/commands/makemessages.py
190 190 f.write(src) 191 191 finally: 192 192 f.close() 193 cmd = 'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 -- from-code UTF-8 -o - "%s"' % (domain, os.path.join(dirpath, thefile))193 cmd = 'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 --from-code UTF-8 -o - "%s"' % (domain, os.path.join(dirpath, thefile)) 194 194 msgs, errors = _popen(cmd) 195 195 if errors: 196 196 raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors)) … … 225 225 raise SyntaxError(msg) 226 226 if verbosity > 1: 227 227 sys.stdout.write('processing file %s in %s\n' % (file, dirpath)) 228 cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 -- from-code UTF-8 -o - "%s"' % (228 cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 --keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 -o - "%s"' % ( 229 229 domain, os.path.join(dirpath, thefile)) 230 230 msgs, errors = _popen(cmd) 231 231 if errors: -
django/views/i18n.py
68 68 function gettext(msgid) { return msgid; } 69 69 function ngettext(singular, plural, count) { return (count == 1) ? singular : plural; } 70 70 function gettext_noop(msgid) { return msgid; } 71 function pgettext(context, msgid) { return msgid; } 72 function npgettext(context, singular, plural, count) { return (count == 1) ? singular : plural; } 71 73 """ 72 74 73 75 LibHead = """ … … 98 100 99 101 function gettext_noop(msgid) { return msgid; } 100 102 103 function pgettext(context, msgid) { 104 var value = gettext(context + '\x04' + msgid); 105 if (value.indexOf('\x04') != -1) { 106 value = msgid; 107 } 108 return value; 109 } 110 111 function npgettext(context, singular, plural, count) { 112 var value = ngettext(context + '\x04' + singular, context + '\x04' + plural, count); 113 if (value.indexOf('\x04') != -1) { 114 value = ngettext(singular, plural, count); 115 } 116 return value; 117 } 101 118 """ 102 119 103 120 LibFormatHead = """ -
django/utils/translation/trans_real.py
279 279 def ugettext(message): 280 280 return do_translate(message, 'ugettext') 281 281 282 def pgettext(context, message): 283 # \x04 is a magic gettext number to separate context from message 284 result = do_translate(u"%s\x04%s" % (context, message), 'ugettext') 285 if "\x04" in result: 286 # Translation not found 287 result = message 288 return result 289 282 290 def gettext_noop(message): 283 291 """ 284 292 Marks strings for translation but doesn't translate them now. This can be … … 313 321 """ 314 322 return do_ntranslate(singular, plural, number, 'ungettext') 315 323 324 def npgettext(context, singular, plural, number): 325 # \x04 is a magic gettext number to separate context from message 326 result = do_ntranslate(u"%s\x04%s" % (context, singular), 327 u"%s\x04%s" % (context, plural), number, 'ungettext') 328 if "\x04" in result: 329 # Translation not found 330 result = do_ntranslate(singular, plural, number, 'ungettext') 331 return result 332 316 333 def check_for_language(lang_code): 317 334 """ 318 335 Checks whether there is a global language file for the given language -
django/utils/translation/__init__.py
10 10 'get_language', 'get_language_bidi', 'get_date_formats', 11 11 'get_partial_date_formats', 'check_for_language', 'to_locale', 12 12 'get_language_from_request', 'templatize', 'ugettext', 'ugettext_lazy', 13 'ungettext', 'deactivate_all'] 13 'ungettext', 'ungettext_lazy', 'pgettext', 'pgettext_lazy', 14 'npgettext', 'npgettext_lazy', 'deactivate_all'] 14 15 15 16 # Here be dragons, so a short explanation of the logic won't hurt: 16 17 # We are trying to solve two problems: (1) access settings, in particular … … 63 64 def ungettext(singular, plural, number): 64 65 return _trans.ungettext(singular, plural, number) 65 66 67 def pgettext(context, message): 68 return _trans.pgettext(context, message) 69 70 def npgettext(context, singular, plural, number): 71 return _trans.npgettext(context, singular, plural, number) 72 66 73 ngettext_lazy = lazy(ngettext, str) 67 74 gettext_lazy = lazy(gettext, str) 68 75 ungettext_lazy = lazy(ungettext, unicode) 69 76 ugettext_lazy = lazy(ugettext, unicode) 77 pgettext_lazy = lazy(pgettext, unicode) 78 npgettext_lazy = lazy(npgettext, unicode) 70 79 71 80 def activate(language): 72 81 return _trans.activate(language) -
django/utils/translation/trans_null.py
15 15 def ungettext(singular, plural, number): 16 16 return force_unicode(ngettext(singular, plural, number)) 17 17 18 def pgettext(context, message): 19 return ugettext(message) 20 21 def npgettext(context, singular, plural, number): 22 return ungettext(singular, plural, number) 23 18 24 activate = lambda x: None 19 25 deactivate = deactivate_all = lambda: None 20 26 get_language = lambda: settings.LANGUAGE_CODE -
tests/regressiontests/views/locale/fr/LC_MESSAGES/djangojs.po
22 22 23 23 msgid "Choose a time" 24 24 msgstr "Choisir une heure" 25 26 msgctxt "month name" 27 msgid "May" 28 msgstr "mai" -
tests/regressiontests/views/tests/i18n.py
30 30 # catalog['this is to be translated'] = 'same_that_trans_txt' 31 31 # javascript_quote is used to be able to check unicode strings 32 32 self.assertContains(response, javascript_quote(trans_txt), 1) 33 if lang_code == 'fr': 34 # Message with context (msgctxt) 35 self.assertContains(response, "['month name\x04May'] = 'mai';", 1) 33 36 34 37 35 38 class JsI18NTests(TestCase): -
tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po
20 20 #: models.py:3 21 21 msgid "Date/time" 22 22 msgstr "Datum/Zeit (LOCALE_PATHS)" 23 24 #: models.py:5 25 msgctxt "month name" 26 msgid "May" 27 msgstr "Mai" 28 29 #: models.py:7 30 msgctxt "verb" 31 msgid "May" 32 msgstr "Kann" 33 34 #: models.py:9 35 msgctxt "search" 36 msgid "%d result" 37 msgid_plural "%d results" 38 msgstr[0] "%d Resultat" 39 msgstr[1] "%d Resultate" -
tests/regressiontests/i18n/tests.py
11 11 from django.utils.formats import get_format, date_format, time_format, localize, localize_input, iter_format_modules 12 12 from django.utils.numberformat import format as nformat 13 13 from django.utils.safestring import mark_safe, SafeString, SafeUnicode 14 from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy, to_locale14 from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy, pgettext, npgettext, to_locale 15 15 from django.utils.importlib import import_module 16 16 17 17 … … 54 54 s2 = pickle.loads(pickle.dumps(s1)) 55 55 self.assertEqual(unicode(s2), "test") 56 56 57 def test_pgettext(self): 58 # Reset translation catalog to include other/locale/de 59 self.old_locale_paths = settings.LOCALE_PATHS 60 settings.LOCALE_PATHS += (os.path.join(os.path.dirname(os.path.abspath(__file__)), 'other', 'locale'),) 61 from django.utils.translation import trans_real 62 trans_real._active = {} 63 trans_real._translations = {} 64 activate('de') 65 66 self.assertEqual(pgettext("unexisting", "May"), u"May") 67 self.assertEqual(pgettext("month name", "May"), u"Mai") 68 self.assertEqual(pgettext("verb", "May"), u"Kann") 69 self.assertEqual(npgettext("search", "%d result", "%d results", 4) % 4, u"4 Resultate") 70 71 settings.LOCALE_PATHS = self.old_locale_paths 72 57 73 def test_string_concat(self): 58 74 """ 59 75 unicode(string_concat(...)) should not raise a TypeError - #4796 -
docs/topics/i18n/internationalization.txt
193 193 ``django-admin.py compilemessages`` or a ``KeyError`` Python exception at 194 194 runtime. 195 195 196 .. _contextual-markers: 197 198 Contextual markers 199 ------------------ 200 201 .. versionadded:: 1.3 202 203 In certain cases, English words may have several meanings, such as ``"May"`` 204 which can refer to a month name or to a verb. If this same word is then used in 205 the code with both meanings, the translator cannot translate it two times 206 differently. To be able to disambiguate such strings, you can use the 207 ``django.utils.translation.pgettext()`` function, or the 208 ``django.utils.translation.npgettext()`` function if the string needs 209 pluralization. 210 211 In the resulting .po file, the string will then appear as often as there are 212 different contextual markers for the same string (the context will appear on the 213 ``msgctxt`` line), allowing the translator to give a different translation for 214 each of them. 215 216 For example: 217 218 from django.utils.translation import pgettext 219 220 month = pgettext("month name", "May") 221 222 which will appear in the .po file as: 223 224 msgctxt "month name" 225 msgid "May" 226 msgstr "" 227 196 228 .. _lazy-translations: 197 229 198 230 Lazy translation -
docs/releases/1.3.txt
86 86 87 87 For more information, see :ref:`transaction-management-functions`. 88 88 89 Contextual markers in translatable strings 90 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 91 92 When a single English word or string appear in your code with different meanings, 93 you can now use the ``pgettext`` function to specify the context of the string. 94 95 For more information, see :ref:`contextual-markers` 96 89 97 Everything else 90 98 ~~~~~~~~~~~~~~~ 91 99