extraction.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. # -*- encoding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import os
  4. import re
  5. import shutil
  6. import warnings
  7. from django.core import management
  8. from django.test import SimpleTestCase
  9. from django.utils.encoding import force_text
  10. from django.utils._os import upath
  11. from django.utils import six
  12. from django.utils.six import StringIO
  13. from django.utils.translation import TranslatorCommentWarning
  14. LOCALE='de'
  15. class ExtractorTests(SimpleTestCase):
  16. PO_FILE='locale/%s/LC_MESSAGES/django.po' % LOCALE
  17. def setUp(self):
  18. self._cwd = os.getcwd()
  19. self.test_dir = os.path.abspath(os.path.dirname(upath(__file__)))
  20. def _rmrf(self, dname):
  21. if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
  22. return
  23. shutil.rmtree(dname)
  24. def tearDown(self):
  25. os.chdir(self.test_dir)
  26. try:
  27. self._rmrf('locale/%s' % LOCALE)
  28. except OSError:
  29. pass
  30. os.chdir(self._cwd)
  31. def assertMsgId(self, msgid, s, use_quotes=True):
  32. q = '"'
  33. if use_quotes:
  34. msgid = '"%s"' % msgid
  35. q = "'"
  36. needle = 'msgid %s' % msgid
  37. msgid = re.escape(msgid)
  38. return self.assertTrue(re.search('^msgid %s' % msgid, s, re.MULTILINE), 'Could not find %(q)s%(n)s%(q)s in generated PO file' % {'n':needle, 'q':q})
  39. def assertNotMsgId(self, msgid, s, use_quotes=True):
  40. if use_quotes:
  41. msgid = '"%s"' % msgid
  42. msgid = re.escape(msgid)
  43. return self.assertTrue(not re.search('^msgid %s' % msgid, s, re.MULTILINE))
  44. class BasicExtractorTests(ExtractorTests):
  45. def test_comments_extractor(self):
  46. os.chdir(self.test_dir)
  47. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  48. self.assertTrue(os.path.exists(self.PO_FILE))
  49. with open(self.PO_FILE, 'r') as fp:
  50. po_contents = force_text(fp.read())
  51. self.assertTrue('#. Translators: This comment should be extracted' in po_contents)
  52. self.assertTrue('This comment should not be extracted' not in po_contents)
  53. # Comments in templates
  54. self.assertTrue('#. Translators: Django template comment for translators' in po_contents)
  55. self.assertTrue("#. Translators: Django comment block for translators\n#. string's meaning unveiled" in po_contents)
  56. self.assertTrue('#. Translators: One-line translator comment #1' in po_contents)
  57. self.assertTrue('#. Translators: Two-line translator comment #1\n#. continued here.' in po_contents)
  58. self.assertTrue('#. Translators: One-line translator comment #2' in po_contents)
  59. self.assertTrue('#. Translators: Two-line translator comment #2\n#. continued here.' in po_contents)
  60. self.assertTrue('#. Translators: One-line translator comment #3' in po_contents)
  61. self.assertTrue('#. Translators: Two-line translator comment #3\n#. continued here.' in po_contents)
  62. self.assertTrue('#. Translators: One-line translator comment #4' in po_contents)
  63. self.assertTrue('#. Translators: Two-line translator comment #4\n#. continued here.' in po_contents)
  64. self.assertTrue('#. Translators: One-line translator comment #5 -- with non ASCII characters: áéíóúö' in po_contents)
  65. self.assertTrue('#. Translators: Two-line translator comment #5 -- with non ASCII characters: áéíóúö\n#. continued here.' in po_contents)
  66. def test_templatize_trans_tag(self):
  67. # ticket #11240
  68. os.chdir(self.test_dir)
  69. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  70. self.assertTrue(os.path.exists(self.PO_FILE))
  71. with open(self.PO_FILE, 'r') as fp:
  72. po_contents = force_text(fp.read())
  73. self.assertMsgId('Literal with a percent symbol at the end %%', po_contents)
  74. self.assertMsgId('Literal with a percent %% symbol in the middle', po_contents)
  75. self.assertMsgId('Completed 50%% of all the tasks', po_contents)
  76. self.assertMsgId('Completed 99%% of all the tasks', po_contents)
  77. self.assertMsgId("Shouldn't double escape this sequence: %% (two percent signs)", po_contents)
  78. self.assertMsgId("Shouldn't double escape this sequence %% either", po_contents)
  79. self.assertMsgId("Looks like a str fmt spec %%s but shouldn't be interpreted as such", po_contents)
  80. self.assertMsgId("Looks like a str fmt spec %% o but shouldn't be interpreted as such", po_contents)
  81. def test_templatize_blocktrans_tag(self):
  82. # ticket #11966
  83. os.chdir(self.test_dir)
  84. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  85. self.assertTrue(os.path.exists(self.PO_FILE))
  86. with open(self.PO_FILE, 'r') as fp:
  87. po_contents = force_text(fp.read())
  88. self.assertMsgId('I think that 100%% is more that 50%% of anything.', po_contents)
  89. self.assertMsgId('I think that 100%% is more that 50%% of %(obj)s.', po_contents)
  90. self.assertMsgId("Blocktrans extraction shouldn't double escape this: %%, a=%(a)s", po_contents)
  91. def test_force_en_us_locale(self):
  92. """Value of locale-munging option used by the command is the right one"""
  93. from django.core.management.commands.makemessages import Command
  94. self.assertTrue(Command.leave_locale_alone)
  95. def test_extraction_error(self):
  96. os.chdir(self.test_dir)
  97. self.assertRaises(SyntaxError, management.call_command, 'makemessages', locale=LOCALE, extensions=['tpl'], verbosity=0)
  98. with self.assertRaises(SyntaxError) as context_manager:
  99. management.call_command('makemessages', locale=LOCALE, extensions=['tpl'], verbosity=0)
  100. six.assertRegex(self, str(context_manager.exception),
  101. r'Translation blocks must not include other block tags: blocktrans \(file templates[/\\]template_with_error\.tpl, line 3\)'
  102. )
  103. # Check that the temporary file was cleaned up
  104. self.assertFalse(os.path.exists('./templates/template_with_error.tpl.py'))
  105. def test_extraction_warning(self):
  106. """test xgettext warning about multiple bare interpolation placeholders"""
  107. os.chdir(self.test_dir)
  108. shutil.copyfile('./code.sample', './code_sample.py')
  109. stdout = StringIO()
  110. management.call_command('makemessages', locale=LOCALE, stdout=stdout)
  111. os.remove('./code_sample.py')
  112. self.assertIn("code_sample.py:4", force_text(stdout.getvalue()))
  113. def test_template_message_context_extractor(self):
  114. """
  115. Ensure that message contexts are correctly extracted for the
  116. {% trans %} and {% blocktrans %} template tags.
  117. Refs #14806.
  118. """
  119. os.chdir(self.test_dir)
  120. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  121. self.assertTrue(os.path.exists(self.PO_FILE))
  122. with open(self.PO_FILE, 'r') as fp:
  123. po_contents = force_text(fp.read())
  124. # {% trans %}
  125. self.assertTrue('msgctxt "Special trans context #1"' in po_contents)
  126. self.assertMsgId("Translatable literal #7a", po_contents)
  127. self.assertTrue('msgctxt "Special trans context #2"' in po_contents)
  128. self.assertMsgId("Translatable literal #7b", po_contents)
  129. self.assertTrue('msgctxt "Special trans context #3"' in po_contents)
  130. self.assertMsgId("Translatable literal #7c", po_contents)
  131. # {% blocktrans %}
  132. self.assertTrue('msgctxt "Special blocktrans context #1"' in po_contents)
  133. self.assertMsgId("Translatable literal #8a", po_contents)
  134. self.assertTrue('msgctxt "Special blocktrans context #2"' in po_contents)
  135. self.assertMsgId("Translatable literal #8b-singular", po_contents)
  136. self.assertTrue("Translatable literal #8b-plural" in po_contents)
  137. self.assertTrue('msgctxt "Special blocktrans context #3"' in po_contents)
  138. self.assertMsgId("Translatable literal #8c-singular", po_contents)
  139. self.assertTrue("Translatable literal #8c-plural" in po_contents)
  140. self.assertTrue('msgctxt "Special blocktrans context #4"' in po_contents)
  141. self.assertMsgId("Translatable literal #8d %(a)s", po_contents)
  142. def test_context_in_single_quotes(self):
  143. os.chdir(self.test_dir)
  144. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  145. self.assertTrue(os.path.exists(self.PO_FILE))
  146. with open(self.PO_FILE, 'r') as fp:
  147. po_contents = force_text(fp.read())
  148. # {% trans %}
  149. self.assertTrue('msgctxt "Context wrapped in double quotes"' in po_contents)
  150. self.assertTrue('msgctxt "Context wrapped in single quotes"' in po_contents)
  151. # {% blocktrans %}
  152. self.assertTrue('msgctxt "Special blocktrans context wrapped in double quotes"' in po_contents)
  153. self.assertTrue('msgctxt "Special blocktrans context wrapped in single quotes"' in po_contents)
  154. def test_template_comments(self):
  155. """Template comment tags on the same line of other constructs (#19552)"""
  156. os.chdir(self.test_dir)
  157. # Test detection/end user reporting of old, incorrect templates
  158. # translator comments syntax
  159. with warnings.catch_warnings(record=True) as ws:
  160. warnings.simplefilter('always')
  161. management.call_command('makemessages', locale=LOCALE, extensions=['thtml'], verbosity=0)
  162. self.assertEqual(len(ws), 3)
  163. for w in ws:
  164. self.assertTrue(issubclass(w.category, TranslatorCommentWarning))
  165. six.assertRegex(self, str(ws[0].message),
  166. r"The translator-targeted comment 'Translators: ignored i18n comment #1' \(file templates/comments.thtml, line 4\) was ignored, because it wasn't the last item on the line\."
  167. )
  168. six.assertRegex(self, str(ws[1].message),
  169. r"The translator-targeted comment 'Translators: ignored i18n comment #3' \(file templates/comments.thtml, line 6\) was ignored, because it wasn't the last item on the line\."
  170. )
  171. six.assertRegex(self, str(ws[2].message),
  172. r"The translator-targeted comment 'Translators: ignored i18n comment #4' \(file templates/comments.thtml, line 8\) was ignored, because it wasn't the last item on the line\."
  173. )
  174. # Now test .po file contents
  175. self.assertTrue(os.path.exists(self.PO_FILE))
  176. with open(self.PO_FILE, 'r') as fp:
  177. po_contents = force_text(fp.read())
  178. self.assertMsgId('Translatable literal #9a', po_contents)
  179. self.assertFalse('ignored comment #1' in po_contents)
  180. self.assertFalse('Translators: ignored i18n comment #1' in po_contents)
  181. self.assertMsgId("Translatable literal #9b", po_contents)
  182. self.assertFalse('ignored i18n comment #2' in po_contents)
  183. self.assertFalse('ignored comment #2' in po_contents)
  184. self.assertMsgId('Translatable literal #9c', po_contents)
  185. self.assertFalse('ignored comment #3' in po_contents)
  186. self.assertFalse('ignored i18n comment #3' in po_contents)
  187. self.assertMsgId('Translatable literal #9d', po_contents)
  188. self.assertFalse('ignored comment #4' in po_contents)
  189. self.assertMsgId('Translatable literal #9e', po_contents)
  190. self.assertFalse('ignored comment #5' in po_contents)
  191. self.assertFalse('ignored i18n comment #4' in po_contents)
  192. self.assertMsgId('Translatable literal #9f', po_contents)
  193. self.assertTrue('#. Translators: valid i18n comment #5' in po_contents)
  194. self.assertMsgId('Translatable literal #9g', po_contents)
  195. self.assertTrue('#. Translators: valid i18n comment #6' in po_contents)
  196. self.assertMsgId('Translatable literal #9h', po_contents)
  197. self.assertTrue('#. Translators: valid i18n comment #7' in po_contents)
  198. self.assertMsgId('Translatable literal #9i', po_contents)
  199. six.assertRegex(self, po_contents, r'#\..+Translators: valid i18n comment #8')
  200. six.assertRegex(self, po_contents, r'#\..+Translators: valid i18n comment #9')
  201. self.assertMsgId("Translatable literal #9j", po_contents)
  202. class JavascriptExtractorTests(ExtractorTests):
  203. PO_FILE='locale/%s/LC_MESSAGES/djangojs.po' % LOCALE
  204. def test_javascript_literals(self):
  205. os.chdir(self.test_dir)
  206. management.call_command('makemessages', domain='djangojs', locale=LOCALE, verbosity=0)
  207. self.assertTrue(os.path.exists(self.PO_FILE))
  208. with open(self.PO_FILE, 'r') as fp:
  209. po_contents = fp.read()
  210. self.assertMsgId('This literal should be included.', po_contents)
  211. self.assertMsgId('This one as well.', po_contents)
  212. self.assertMsgId(r'He said, \"hello\".', po_contents)
  213. self.assertMsgId("okkkk", po_contents)
  214. self.assertMsgId("TEXT", po_contents)
  215. self.assertMsgId("It's at http://example.com", po_contents)
  216. self.assertMsgId("String", po_contents)
  217. self.assertMsgId("/* but this one will be too */ 'cause there is no way of telling...", po_contents)
  218. self.assertMsgId("foo", po_contents)
  219. self.assertMsgId("bar", po_contents)
  220. self.assertMsgId("baz", po_contents)
  221. self.assertMsgId("quz", po_contents)
  222. self.assertMsgId("foobar", po_contents)
  223. class IgnoredExtractorTests(ExtractorTests):
  224. def test_ignore_option(self):
  225. os.chdir(self.test_dir)
  226. pattern1 = os.path.join('ignore_dir', '*')
  227. stdout = StringIO()
  228. management.call_command('makemessages', locale=LOCALE, verbosity=2,
  229. ignore_patterns=[pattern1], stdout=stdout)
  230. data = stdout.getvalue()
  231. self.assertTrue("ignoring directory ignore_dir" in data)
  232. self.assertTrue(os.path.exists(self.PO_FILE))
  233. with open(self.PO_FILE, 'r') as fp:
  234. po_contents = fp.read()
  235. self.assertMsgId('This literal should be included.', po_contents)
  236. self.assertNotMsgId('This should be ignored.', po_contents)
  237. class SymlinkExtractorTests(ExtractorTests):
  238. def setUp(self):
  239. self._cwd = os.getcwd()
  240. self.test_dir = os.path.abspath(os.path.dirname(upath(__file__)))
  241. self.symlinked_dir = os.path.join(self.test_dir, 'templates_symlinked')
  242. def tearDown(self):
  243. super(SymlinkExtractorTests, self).tearDown()
  244. os.chdir(self.test_dir)
  245. try:
  246. os.remove(self.symlinked_dir)
  247. except OSError:
  248. pass
  249. os.chdir(self._cwd)
  250. def test_symlink(self):
  251. if hasattr(os, 'symlink'):
  252. if os.path.exists(self.symlinked_dir):
  253. self.assertTrue(os.path.islink(self.symlinked_dir))
  254. else:
  255. os.symlink(os.path.join(self.test_dir, 'templates'), self.symlinked_dir)
  256. os.chdir(self.test_dir)
  257. management.call_command('makemessages', locale=LOCALE, verbosity=0, symlinks=True)
  258. self.assertTrue(os.path.exists(self.PO_FILE))
  259. with open(self.PO_FILE, 'r') as fp:
  260. po_contents = force_text(fp.read())
  261. self.assertMsgId('This literal should be included.', po_contents)
  262. self.assertTrue('templates_symlinked/test.html' in po_contents)
  263. class CopyPluralFormsExtractorTests(ExtractorTests):
  264. def test_copy_plural_forms(self):
  265. os.chdir(self.test_dir)
  266. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  267. self.assertTrue(os.path.exists(self.PO_FILE))
  268. with open(self.PO_FILE, 'r') as fp:
  269. po_contents = force_text(fp.read())
  270. self.assertTrue('Plural-Forms: nplurals=2; plural=(n != 1)' in po_contents)
  271. class NoWrapExtractorTests(ExtractorTests):
  272. def test_no_wrap_enabled(self):
  273. os.chdir(self.test_dir)
  274. management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=True)
  275. self.assertTrue(os.path.exists(self.PO_FILE))
  276. with open(self.PO_FILE, 'r') as fp:
  277. po_contents = force_text(fp.read())
  278. self.assertMsgId('This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option.', po_contents)
  279. def test_no_wrap_disabled(self):
  280. os.chdir(self.test_dir)
  281. management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=False)
  282. self.assertTrue(os.path.exists(self.PO_FILE))
  283. with open(self.PO_FILE, 'r') as fp:
  284. po_contents = force_text(fp.read())
  285. 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)
  286. class NoLocationExtractorTests(ExtractorTests):
  287. def test_no_location_enabled(self):
  288. os.chdir(self.test_dir)
  289. management.call_command('makemessages', locale=LOCALE, verbosity=0, no_location=True)
  290. self.assertTrue(os.path.exists(self.PO_FILE))
  291. with open(self.PO_FILE, 'r') as fp:
  292. po_contents = force_text(fp.read())
  293. self.assertFalse('#: templates/test.html:55' in po_contents)
  294. def test_no_location_disabled(self):
  295. os.chdir(self.test_dir)
  296. management.call_command('makemessages', locale=LOCALE, verbosity=0, no_location=False)
  297. self.assertTrue(os.path.exists(self.PO_FILE))
  298. with open(self.PO_FILE, 'r') as fp:
  299. po_contents = force_text(fp.read())
  300. self.assertTrue('#: templates/test.html:55' in po_contents)
  301. class KeepPotFileExtractorTests(ExtractorTests):
  302. POT_FILE='locale/django.pot'
  303. def setUp(self):
  304. super(KeepPotFileExtractorTests, self).setUp()
  305. def tearDown(self):
  306. super(KeepPotFileExtractorTests, self).tearDown()
  307. os.chdir(self.test_dir)
  308. try:
  309. os.unlink(self.POT_FILE)
  310. except OSError:
  311. pass
  312. os.chdir(self._cwd)
  313. def test_keep_pot_disabled_by_default(self):
  314. os.chdir(self.test_dir)
  315. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  316. self.assertFalse(os.path.exists(self.POT_FILE))
  317. def test_keep_pot_explicitly_disabled(self):
  318. os.chdir(self.test_dir)
  319. management.call_command('makemessages', locale=LOCALE, verbosity=0,
  320. keep_pot=False)
  321. self.assertFalse(os.path.exists(self.POT_FILE))
  322. def test_keep_pot_enabled(self):
  323. os.chdir(self.test_dir)
  324. management.call_command('makemessages', locale=LOCALE, verbosity=0,
  325. keep_pot=True)
  326. self.assertTrue(os.path.exists(self.POT_FILE))
  327. class MultipleLocaleExtractionTests(ExtractorTests):
  328. PO_FILE_PT = 'locale/pt/LC_MESSAGES/django.po'
  329. PO_FILE_DE = 'locale/de/LC_MESSAGES/django.po'
  330. LOCALES = ['pt', 'de', 'ch']
  331. def tearDown(self):
  332. os.chdir(self.test_dir)
  333. for locale in self.LOCALES:
  334. try:
  335. self._rmrf('locale/%s' % locale)
  336. except OSError:
  337. pass
  338. os.chdir(self._cwd)
  339. def test_multiple_locales(self):
  340. os.chdir(self.test_dir)
  341. management.call_command('makemessages', locale=['pt','de'], verbosity=0)
  342. self.assertTrue(os.path.exists(self.PO_FILE_PT))
  343. self.assertTrue(os.path.exists(self.PO_FILE_DE))
  344. def test_comma_separated_locales(self):
  345. os.chdir(self.test_dir)
  346. management.call_command('makemessages', locale='pt,de,ch', verbosity=0)
  347. self.assertTrue(os.path.exists(self.PO_FILE_PT))
  348. self.assertTrue(os.path.exists(self.PO_FILE_DE))