compilation.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import os
  2. from io import BytesIO
  3. from django.core.management import CommandError
  4. from django.core.management.commands.compilemessages import compile_messages
  5. from django.test import TestCase
  6. from django.test.utils import override_settings
  7. from django.utils import translation
  8. test_dir = os.path.abspath(os.path.dirname(__file__))
  9. class MessageCompilationTests(TestCase):
  10. def setUp(self):
  11. self._cwd = os.getcwd()
  12. def tearDown(self):
  13. os.chdir(self._cwd)
  14. class PoFileTests(MessageCompilationTests):
  15. LOCALE='es_AR'
  16. MO_FILE='locale/%s/LC_MESSAGES/django.mo' % LOCALE
  17. def test_bom_rejection(self):
  18. os.chdir(test_dir)
  19. # We don't use the django.core.management infrastructure (call_command()
  20. # et al) because CommandError's cause exit(1) there. We test the
  21. # underlying compile_messages function instead
  22. out = BytesIO()
  23. self.assertRaises(CommandError, compile_messages, out, locale=self.LOCALE)
  24. self.assertFalse(os.path.exists(self.MO_FILE))
  25. class PoFileContentsTests(MessageCompilationTests):
  26. # Ticket #11240
  27. LOCALE='fr'
  28. MO_FILE='locale/%s/LC_MESSAGES/django.mo' % LOCALE
  29. def setUp(self):
  30. super(PoFileContentsTests, self).setUp()
  31. self.addCleanup(os.unlink, os.path.join(test_dir, self.MO_FILE))
  32. def test_percent_symbol_in_po_file(self):
  33. os.chdir(test_dir)
  34. # We don't use the django.core.management infrastructure (call_command()
  35. # et al) because CommandError's cause exit(1) there. We test the
  36. # underlying compile_messages function instead
  37. out = BytesIO()
  38. compile_messages(out, locale=self.LOCALE)
  39. self.assertTrue(os.path.exists(self.MO_FILE))
  40. class PercentRenderingTests(MessageCompilationTests):
  41. # Ticket #11240 -- Testing rendering doesn't belong here but we are trying
  42. # to keep tests for all the stack together
  43. LOCALE='it'
  44. MO_FILE='locale/%s/LC_MESSAGES/django.mo' % LOCALE
  45. @override_settings(LOCALE_PATHS=(os.path.join(test_dir, 'locale'),))
  46. def test_percent_symbol_escaping(self):
  47. from django.template import Template, Context
  48. os.chdir(test_dir)
  49. # We don't use the django.core.management infrastructure (call_command()
  50. # et al) because CommandError's cause exit(1) there. We test the
  51. # underlying compile_messages function instead
  52. out = BytesIO()
  53. compile_messages(out, locale=self.LOCALE)
  54. with translation.override(self.LOCALE):
  55. t = Template('{% load i18n %}{% trans "Looks like a str fmt spec %% o but shouldn\'t be interpreted as such" %}')
  56. rendered = t.render(Context({}))
  57. self.assertEqual(rendered, 'IT translation contains %% for the above string')
  58. t = Template('{% load i18n %}{% trans "Completed 50%% of all the tasks" %}')
  59. rendered = t.render(Context({}))
  60. self.assertEqual(rendered, 'IT translation of Completed 50%% of all the tasks')