Ticket #15495: ticket15495.diff

File ticket15495.diff, 7.2 KB (added by Łukasz Rekucki, 14 years ago)

A bit naive patch with tests.

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

    diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py
    index a244a60..48d761a 100644
    a b from subprocess import PIPE, Popen  
    1010from django.core.management.base import CommandError, NoArgsCommand
    1111from django.utils.text import get_text_list
    1212
    13 pythonize_re = re.compile(r'(?:^|\n)\s*//')
    1413plural_forms_re = re.compile(r'^(?P<value>"Plural-Forms.+?\\n")\s*$', re.MULTILINE | re.DOTALL)
    1514
     15# this removes everything after comment, so the next regexp
     16# doesn't messup anything
     17jspythonize_re = re.compile(r'(?:^|\n)\s*//.*')
     18jsblockcomments_re = re.compile(r'/\*(.+?)\*/', re.DOTALL)
     19
     20def pythonizejs(text):
     21    def block_comment_replacement(m):
     22        nlcount = m.group(1).count('\n')
     23        if not nlcount:
     24            return ""
     25        return '\n'.join("#" * (nlcount + 1))
     26    text = jspythonize_re.sub('\n#', text)
     27    text = jsblockcomments_re.sub(block_comment_replacement, text)
     28    return text
     29
    1630def handle_extensions(extensions=('html',)):
    1731    """
    1832    organizes multiple extensions that are separated with commas or passed by
    def make_messages(locale=None, domain='django', verbosity='1', all=False,  
    184198                if verbosity > 1:
    185199                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
    186200                src = open(os.path.join(dirpath, file), "rU").read()
    187                 src = pythonize_re.sub('\n#', src)
     201                src = pythonizejs(src)
    188202                thefile = '%s.py' % file
    189203                f = open(os.path.join(dirpath, thefile), "w")
    190204                try:
  • tests/regressiontests/i18n/commands/extraction.py

    diff --git a/tests/regressiontests/i18n/commands/extraction.py b/tests/regressiontests/i18n/commands/extraction.py
    index f17bb47..46a816d 100644
    a b class ExtractorTests(TestCase):  
    2727            pass
    2828        os.chdir(self._cwd)
    2929
    30     def assertMsgId(self, msgid, s, use_quotes=True):
     30    def assertMsgId(self, msgid, text, use_quotes=True):
    3131        if use_quotes:
    3232            msgid = '"%s"' % msgid
    33         return self.assert_(re.search('^msgid %s' % msgid, s, re.MULTILINE))
     33        self.assertRegexpMatches(text, '(?m)^msgid %s' % re.escape(msgid))
    3434
    35     def assertNotMsgId(self, msgid, s, use_quotes=True):
     35    def assertMsgIdLineNumber(self, msgid, linenumber, text, use_quotes=True):
    3636        if use_quotes:
    3737            msgid = '"%s"' % msgid
    38         return self.assert_(not re.search('^msgid %s' % msgid, s, re.MULTILINE))
     38        m = re.search('^#: .+:(\d+)\n^msgid %s' % re.escape(msgid), text, re.M)
     39        self.assertIsNotNone(m)
     40        self.assertEqual(int(m.group(1)), linenumber)
     41
     42    def assertNotMsgId(self, msgid, text, use_quotes=True):
     43        if use_quotes:
     44            msgid = '"%s"' % msgid
     45        self.assertNotRegexpMatches(text, '(?m)^msgid %s' % re.escape(msgid))
    3946
    4047
    4148class BasicExtractorTests(ExtractorTests):
    class BasicExtractorTests(ExtractorTests):  
    5764        self.assert_(os.path.exists(self.PO_FILE))
    5865        po_contents = open(self.PO_FILE, 'r').read()
    5966        self.assertMsgId('I think that 100%% is more that 50%% of anything.', po_contents)
    60         self.assertMsgId('I think that 100%% is more that 50%% of %\(obj\)s.', po_contents)
     67        self.assertMsgId('I think that 100%% is more that 50%% of %(obj)s.', po_contents)
    6168
    6269    def test_extraction_error(self):
    6370        os.chdir(self.test_dir)
    class JavascriptExtractorTests(ExtractorTests):  
    7986
    8087    def test_javascript_literals(self):
    8188        os.chdir(self.test_dir)
    82         management.call_command('makemessages', domain='djangojs', locale=LOCALE, verbosity=0)
    83         self.assert_(os.path.exists(self.PO_FILE))
    84         po_contents = open(self.PO_FILE, 'r').read()
    85         self.assertMsgId('This literal should be included.', po_contents)
    86         self.assertMsgId('This one as well.', po_contents)
     89        shutil.copyfile('./javascript/literals.txt', './javascript.js')
     90        try:
     91            management.call_command('makemessages', domain='djangojs', locale=LOCALE, verbosity=0)
     92            self.assert_(os.path.exists(self.PO_FILE))
     93            po_contents = open(self.PO_FILE, 'r').read()
     94            self.assertMsgId('This literal should be included.', po_contents)
     95            self.assertMsgId('This one as well.', po_contents)
     96            self.assertMsgId('This too', po_contents)
     97            self.assertNotMsgId('Ignore', po_contents)
     98        finally:
     99            os.remove('./javascript.js')
    87100
     101    def test_javascript_block_comments(self):
     102        os.chdir(self.test_dir)
     103        shutil.copyfile('./javascript/block_comments.txt', './javascript.js')
     104        try:
     105            management.call_command('makemessages', domain='djangojs', locale=LOCALE, verbosity=0)
     106            self.assert_(os.path.exists(self.PO_FILE))
     107            po_contents = open(self.PO_FILE, 'r').read()
     108            self.assertMsgId('Line1', po_contents)
     109            self.assertMsgId('Line6A', po_contents)
     110            self.assertMsgId('Line6B', po_contents)
     111            self.assertMsgId('Line8', po_contents)
     112            self.assertMsgId('Line12', po_contents)
     113            self.assertNotMsgId('Ignore', po_contents)
     114            # check line number
     115            self.assertMsgIdLineNumber('Line1', 1, po_contents)
     116            self.assertMsgIdLineNumber('Line6A', 6, po_contents)
     117            self.assertMsgIdLineNumber('Line6B', 6, po_contents)
     118            self.assertMsgIdLineNumber('Line8', 8, po_contents)
     119            self.assertMsgIdLineNumber('Line12', 12, po_contents)
     120        finally:
     121            os.remove('./javascript.js')
    88122
    89123class IgnoredExtractorTests(ExtractorTests):
    90124
  • deleted file tests/regressiontests/i18n/commands/javascript.js

    diff --git a/tests/regressiontests/i18n/commands/javascript.js b/tests/regressiontests/i18n/commands/javascript.js
    deleted file mode 100644
    index bc5ec87..0000000
    + -  
    1 // '
    2 gettext('This literal should be included.')
    3 // '
    4 gettext('This one as well.')
  • new file tests/regressiontests/i18n/commands/javascript/block_comments.txt

    diff --git a/tests/regressiontests/i18n/commands/javascript/block_comments.txt b/tests/regressiontests/i18n/commands/javascript/block_comments.txt
    new file mode 100644
    index 0000000..843e840
    - +  
     1/* / ' */ gettext("Line1");
     2function() {
     3  /*
     4   * Some long comment/message that doesn't do anything
     5   */
     6   gettext("Line6A"); /* */ gettext("Line6B");
     7   /* one line comment */
     8   gettext("Line8");
     9   /*
     10    * Another comment with magic sequence / '
     11    */
     12    gettext("Line12");
     13   /* gettext("Ignore") */
     14}
     15 No newline at end of file
  • new file tests/regressiontests/i18n/commands/javascript/literals.txt

    diff --git a/tests/regressiontests/i18n/commands/javascript/literals.txt b/tests/regressiontests/i18n/commands/javascript/literals.txt
    new file mode 100644
    index 0000000..362d34c
    - +  
     1// '
     2gettext('This literal should be included.')
     3// '
     4gettext('This one as well.')
     5// /* fake comment start
     6gettext('This too')
     7// fake end */ gettext('Ignore')
     8 No newline at end of file
Back to Top