extraction.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import os
  2. import re
  3. import shutil
  4. from django.test import TestCase
  5. from django.core import management
  6. LOCALE='de'
  7. class ExtractorTests(TestCase):
  8. PO_FILE='locale/%s/LC_MESSAGES/django.po' % LOCALE
  9. def setUp(self):
  10. self._cwd = os.getcwd()
  11. self.test_dir = os.path.abspath(os.path.dirname(__file__))
  12. def _rmrf(self, dname):
  13. if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
  14. return
  15. shutil.rmtree(dname)
  16. def tearDown(self):
  17. os.chdir(self.test_dir)
  18. try:
  19. self._rmrf('locale/%s' % LOCALE)
  20. except OSError:
  21. pass
  22. os.chdir(self._cwd)
  23. def assertMsgId(self, msgid, s, use_quotes=True):
  24. if use_quotes:
  25. msgid = '"%s"' % msgid
  26. return self.assertTrue(re.search('^msgid %s' % msgid, s, re.MULTILINE))
  27. def assertNotMsgId(self, msgid, s, use_quotes=True):
  28. if use_quotes:
  29. msgid = '"%s"' % msgid
  30. return self.assertTrue(not re.search('^msgid %s' % msgid, s, re.MULTILINE))
  31. class BasicExtractorTests(ExtractorTests):
  32. def test_comments_extractor(self):
  33. os.chdir(self.test_dir)
  34. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  35. self.assertTrue(os.path.exists(self.PO_FILE))
  36. po_contents = open(self.PO_FILE, 'r').read()
  37. self.assertTrue('#. Translators: This comment should be extracted' in po_contents)
  38. self.assertTrue('This comment should not be extracted' not in po_contents)
  39. # Comments in templates
  40. self.assertTrue('#. Translators: Django template comment for translators' in po_contents)
  41. self.assertTrue('#. Translators: Django comment block for translators' in po_contents)
  42. def test_templatize(self):
  43. os.chdir(self.test_dir)
  44. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  45. self.assertTrue(os.path.exists(self.PO_FILE))
  46. po_contents = open(self.PO_FILE, 'r').read()
  47. self.assertMsgId('I think that 100%% is more that 50%% of anything.', po_contents)
  48. self.assertMsgId('I think that 100%% is more that 50%% of %\(obj\)s.', po_contents)
  49. def test_extraction_error(self):
  50. os.chdir(self.test_dir)
  51. shutil.copyfile('./templates/template_with_error.txt', './templates/template_with_error.html')
  52. self.assertRaises(SyntaxError, management.call_command, 'makemessages', locale=LOCALE, verbosity=0)
  53. try: # TODO: Simplify this try/try block when we drop support for Python 2.4
  54. try:
  55. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  56. except SyntaxError, e:
  57. self.assertEqual(str(e), 'Translation blocks must not include other block tags: blocktrans (file templates/template_with_error.html, line 3)')
  58. finally:
  59. os.remove('./templates/template_with_error.html')
  60. os.remove('./templates/template_with_error.html.py') # Waiting for #8536 to be fixed
  61. class JavascriptExtractorTests(ExtractorTests):
  62. PO_FILE='locale/%s/LC_MESSAGES/djangojs.po' % LOCALE
  63. def test_javascript_literals(self):
  64. os.chdir(self.test_dir)
  65. management.call_command('makemessages', domain='djangojs', locale=LOCALE, verbosity=0)
  66. self.assertTrue(os.path.exists(self.PO_FILE))
  67. po_contents = open(self.PO_FILE, 'r').read()
  68. self.assertMsgId('This literal should be included.', po_contents)
  69. self.assertMsgId('This one as well.', po_contents)
  70. class IgnoredExtractorTests(ExtractorTests):
  71. def test_ignore_option(self):
  72. os.chdir(self.test_dir)
  73. management.call_command('makemessages', locale=LOCALE, verbosity=0, ignore_patterns=['ignore_dir/*'])
  74. self.assertTrue(os.path.exists(self.PO_FILE))
  75. po_contents = open(self.PO_FILE, 'r').read()
  76. self.assertMsgId('This literal should be included.', po_contents)
  77. self.assertNotMsgId('This should be ignored.', po_contents)
  78. class SymlinkExtractorTests(ExtractorTests):
  79. def setUp(self):
  80. self._cwd = os.getcwd()
  81. self.test_dir = os.path.abspath(os.path.dirname(__file__))
  82. self.symlinked_dir = os.path.join(self.test_dir, 'templates_symlinked')
  83. def tearDown(self):
  84. super(SymlinkExtractorTests, self).tearDown()
  85. os.chdir(self.test_dir)
  86. try:
  87. os.remove(self.symlinked_dir)
  88. except OSError:
  89. pass
  90. os.chdir(self._cwd)
  91. def test_symlink(self):
  92. if hasattr(os, 'symlink'):
  93. if os.path.exists(self.symlinked_dir):
  94. self.assertTrue(os.path.islink(self.symlinked_dir))
  95. else:
  96. os.symlink(os.path.join(self.test_dir, 'templates'), self.symlinked_dir)
  97. os.chdir(self.test_dir)
  98. management.call_command('makemessages', locale=LOCALE, verbosity=0, symlinks=True)
  99. self.assertTrue(os.path.exists(self.PO_FILE))
  100. po_contents = open(self.PO_FILE, 'r').read()
  101. self.assertMsgId('This literal should be included.', po_contents)
  102. self.assertTrue('templates_symlinked/test.html' in po_contents)
  103. class CopyPluralFormsExtractorTests(ExtractorTests):
  104. def test_copy_plural_forms(self):
  105. os.chdir(self.test_dir)
  106. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  107. self.assertTrue(os.path.exists(self.PO_FILE))
  108. po_contents = open(self.PO_FILE, 'r').read()
  109. self.assertTrue('Plural-Forms: nplurals=2; plural=(n != 1)' in po_contents)
  110. class NoWrapExtractorTests(ExtractorTests):
  111. def test_no_wrap_enabled(self):
  112. os.chdir(self.test_dir)
  113. management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=True)
  114. self.assertTrue(os.path.exists(self.PO_FILE))
  115. po_contents = open(self.PO_FILE, 'r').read()
  116. self.assertMsgId('This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option.', po_contents)
  117. def test_no_wrap_disabled(self):
  118. os.chdir(self.test_dir)
  119. management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=False)
  120. self.assertTrue(os.path.exists(self.PO_FILE))
  121. po_contents = open(self.PO_FILE, 'r').read()
  122. 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)