Ticket #10004: comment-extraction.patch

File comment-extraction.patch, 7.3 KB (added by Claude Paroz, 9 years ago)

Add comment extraction to makemessages with L10n keyword

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

     
    196196                    'xgettext -d %s -L Perl %s --keyword=gettext_noop '
    197197                    '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
    198198                    '--keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 '
    199                     '--from-code UTF-8 -o - "%s"' % (
     199                    '--from-code UTF-8 --add-comments=L10n -o - "%s"' % (
    200200                        domain, wrap, os.path.join(dirpath, thefile)
    201201                    )
    202202                )
     
    240240                    '--keyword=ugettext_noop --keyword=ugettext_lazy '
    241241                    '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
    242242                    '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
    243                     '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 -o - '
    244                     '"%s"' % (domain, wrap, os.path.join(dirpath, thefile))
     243                    '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 '
     244                    '--add-comments=L10n -o - "%s"' % (
     245                        domain, wrap, os.path.join(dirpath, thefile))
    245246                )
    246247                msgs, errors = _popen(cmd)
    247248                if errors:
  • tests/regressiontests/i18n/commands/__init__.py

     
     1from django.utils.translation import ugettext as _
     2
     3# L10n: This comment should be extracted
     4dummy1 = _("This is a translatable string.")
     5
     6# This comment should not be extracted
     7dummy2 = _("This is another translatable string.")
     8
  • tests/regressiontests/i18n/commands/extraction.py

     
    88
    99class ExtractorTests(TestCase):
    1010
    11     PO_FILE='locale/%s/LC_MESSAGES/django.po' % LOCALE
     11    PO_FILE    ='locale/%s/LC_MESSAGES/django.po' % LOCALE
     12    JS_PO_FILE ='locale/%s/LC_MESSAGES/djangojs.po' % LOCALE
    1213
    1314    def setUp(self):
    1415        self._cwd = os.getcwd()
     
    3839        return self.assert_(not re.search('^msgid %s' % msgid, s, re.MULTILINE))
    3940
    4041
    41 class TemplateExtractorTests(ExtractorTests):
     42class BasicExtractorTests(ExtractorTests):
    4243
    43     def test_templatize(self):
     44    def setUp(self):
     45        super(BasicExtractorTests, self).setUp()
    4446        os.chdir(self.test_dir)
     47
     48    def test_templatize(self):
    4549        management.call_command('makemessages', locale=LOCALE, verbosity=0)
    4650        self.assert_(os.path.exists(self.PO_FILE))
    4751        po_contents = open(self.PO_FILE, 'r').read()
    4852        self.assertMsgId('I think that 100%% is more that 50%% of anything.', po_contents)
    4953        self.assertMsgId('I think that 100%% is more that 50%% of %\(obj\)s.', po_contents)
    5054
     55    def test_copy_plural_forms(self):
     56        management.call_command('makemessages', locale=LOCALE, verbosity=0)
     57        self.assert_(os.path.exists(self.PO_FILE))
     58        po_contents = open(self.PO_FILE, 'r').read()
     59        self.assert_('Plural-Forms: nplurals=2; plural=(n != 1)' in po_contents)
    5160
    52 class JavascriptExtractorTests(ExtractorTests):
     61    def test_comments_extractor(self):
     62        management.call_command('makemessages', locale=LOCALE, verbosity=0)
     63        self.assert_(os.path.exists(self.PO_FILE))
     64        po_contents = open(self.PO_FILE, 'r').read()
     65        self.assert_('#. L10n: This comment should be extracted' in po_contents)
     66        self.assert_('This comment should not be extracted' not in po_contents)
    5367
    54     PO_FILE='locale/%s/LC_MESSAGES/djangojs.po' % LOCALE
    55 
    5668    def test_javascript_literals(self):
    57         os.chdir(self.test_dir)
    5869        management.call_command('makemessages', domain='djangojs', locale=LOCALE, verbosity=0)
    59         self.assert_(os.path.exists(self.PO_FILE))
    60         po_contents = open(self.PO_FILE, 'r').read()
     70        self.assert_(os.path.exists(self.JS_PO_FILE))
     71        po_contents = open(self.JS_PO_FILE, 'r').read()
    6172        self.assertMsgId('This literal should be included.', po_contents)
    6273        self.assertMsgId('This one as well.', po_contents)
    6374
    64 
    65 class IgnoredExtractorTests(ExtractorTests):
    66 
    6775    def test_ignore_option(self):
    68         os.chdir(self.test_dir)
    6976        management.call_command('makemessages', locale=LOCALE, verbosity=0, ignore_patterns=['ignore_dir/*'])
    7077        self.assert_(os.path.exists(self.PO_FILE))
    7178        po_contents = open(self.PO_FILE, 'r').read()
    7279        self.assertMsgId('This literal should be included.', po_contents)
    7380        self.assertNotMsgId('This should be ignored.', po_contents)
    7481
     82    def test_no_wrap_enabled(self):
     83        management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=True)
     84        self.assert_(os.path.exists(self.PO_FILE))
     85        po_contents = open(self.PO_FILE, 'r').read()
     86        self.assertMsgId('This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option.', po_contents)
    7587
     88    def test_no_wrap_disabled(self):
     89        management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=False)
     90        self.assert_(os.path.exists(self.PO_FILE))
     91        po_contents = open(self.PO_FILE, 'r').read()
     92        self.assertMsgId('""\n"This literal should also be included wrapped or not wrapped depending on the "\n"use of the --no-wrap option."', po_contents, use_quotes=False)
     93
     94
    7695class SymlinkExtractorTests(ExtractorTests):
    7796
    7897    def setUp(self):
     
    101120            po_contents = open(self.PO_FILE, 'r').read()
    102121            self.assertMsgId('This literal should be included.', po_contents)
    103122            self.assert_('templates_symlinked/test.html' in po_contents)
    104 
    105 
    106 class CopyPluralFormsExtractorTests(ExtractorTests):
    107 
    108     def test_copy_plural_forms(self):
    109         os.chdir(self.test_dir)
    110         management.call_command('makemessages', locale=LOCALE, verbosity=0)
    111         self.assert_(os.path.exists(self.PO_FILE))
    112         po_contents = open(self.PO_FILE, 'r').read()
    113         self.assert_('Plural-Forms: nplurals=2; plural=(n != 1)' in po_contents)
    114 
    115 
    116 class NoWrapExtractorTests(ExtractorTests):
    117 
    118     def test_no_wrap_enabled(self):
    119         os.chdir(self.test_dir)
    120         management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=True)
    121         self.assert_(os.path.exists(self.PO_FILE))
    122         po_contents = open(self.PO_FILE, 'r').read()
    123         self.assertMsgId('This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option.', po_contents)
    124 
    125     def test_no_wrap_disabled(self):
    126         os.chdir(self.test_dir)
    127         management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=False)
    128         self.assert_(os.path.exists(self.PO_FILE))
    129         po_contents = open(self.PO_FILE, 'r').read()
    130         self.assertMsgId('""\n"This literal should also be included wrapped or not wrapped depending on the "\n"use of the --no-wrap option."', po_contents, use_quotes=False)
Back to Top