Ticket #9988: pgettext.patch

File pgettext.patch, 6.9 KB (added by Claude Paroz, 14 years ago)

pgettext implementation

  • django/core/management/commands/makemessages.py

     
    225225                        raise SyntaxError(msg)
    226226                if verbosity > 1:
    227227                    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"' % (
    229229                    domain, os.path.join(dirpath, thefile))
    230230                msgs, errors = _popen(cmd)
    231231                if errors:
  • django/utils/translation/trans_real.py

     
    279279def ugettext(message):
    280280    return do_translate(message, 'ugettext')
    281281
     282def 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
    282290def gettext_noop(message):
    283291    """
    284292    Marks strings for translation but doesn't translate them now. This can be
     
    313321    """
    314322    return do_ntranslate(singular, plural, number, 'ungettext')
    315323
     324def 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
    316333def check_for_language(lang_code):
    317334    """
    318335    Checks whether there is a global language file for the given language
  • django/utils/translation/__init__.py

     
    1010        'get_language', 'get_language_bidi', 'get_date_formats',
    1111        'get_partial_date_formats', 'check_for_language', 'to_locale',
    1212        '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']
    1415
    1516# Here be dragons, so a short explanation of the logic won't hurt:
    1617# We are trying to solve two problems: (1) access settings, in particular
     
    6364def ungettext(singular, plural, number):
    6465    return _trans.ungettext(singular, plural, number)
    6566
     67def pgettext(context, message):
     68    return _trans.pgettext(context, message)
     69
     70def npgettext(context, singular, plural, number):
     71    return _trans.npgettext(context, singular, plural, number)
     72
    6673ngettext_lazy = lazy(ngettext, str)
    6774gettext_lazy = lazy(gettext, str)
    6875ungettext_lazy = lazy(ungettext, unicode)
    6976ugettext_lazy = lazy(ugettext, unicode)
     77pgettext_lazy = lazy(pgettext, unicode)
     78npgettext_lazy = lazy(npgettext, unicode)
    7079
    7180def activate(language):
    7281    return _trans.activate(language)
  • django/utils/translation/trans_null.py

     
    1515def ungettext(singular, plural, number):
    1616    return force_unicode(ngettext(singular, plural, number))
    1717
     18def pgettext(context, message):
     19    return ugettext(message)
     20
     21def npgettext(context, singular, plural, number):
     22    return ungettext(singular, plural, number)
     23
    1824activate = lambda x: None
    1925deactivate = deactivate_all = lambda: None
    2026get_language = lambda: settings.LANGUAGE_CODE
  • tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po

     
    2020#: models.py:3
    2121msgid "Date/time"
    2222msgstr "Datum/Zeit (LOCALE_PATHS)"
     23
     24#: models.py:5
     25msgctxt "month name"
     26msgid "May"
     27msgstr "Mai"
     28
     29#: models.py:7
     30msgctxt "verb"
     31msgid "May"
     32msgstr "Kann"
     33
     34#: models.py:9
     35msgctxt "search"
     36msgid "%d result"
     37msgid_plural "%d results"
     38msgstr[0] "%d Resultat"
     39msgstr[1] "%d Resultate"
  • tests/regressiontests/i18n/tests.py

     
    1111from django.utils.formats import get_format, date_format, time_format, localize, localize_input, iter_format_modules
    1212from django.utils.numberformat import format as nformat
    1313from django.utils.safestring import mark_safe, SafeString, SafeUnicode
    14 from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy, to_locale
     14from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy, pgettext, npgettext, to_locale
    1515from django.utils.importlib import import_module
    1616
    1717
     
    5454        s2 = pickle.loads(pickle.dumps(s1))
    5555        self.assertEqual(unicode(s2), "test")
    5656
     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
    5773    def test_string_concat(self):
    5874        """
    5975        unicode(string_concat(...)) should not raise a TypeError - #4796
Back to Top