2010-10-10 16:38:28 +00:00
|
|
|
import os
|
2013-10-28 13:17:48 +00:00
|
|
|
import unittest
|
2010-10-10 16:38:28 +00:00
|
|
|
|
2012-05-27 21:11:27 +00:00
|
|
|
from django.core.management import call_command, CommandError
|
2013-10-28 13:17:48 +00:00
|
|
|
from django.core.management.utils import find_command
|
2013-01-17 03:35:46 +00:00
|
|
|
from django.test import SimpleTestCase
|
2011-12-11 00:07:06 +00:00
|
|
|
from django.test.utils import override_settings
|
2013-01-17 03:35:46 +00:00
|
|
|
from django.utils import translation
|
2012-12-08 10:13:52 +00:00
|
|
|
from django.utils._os import upath
|
2012-08-08 21:40:20 +00:00
|
|
|
from django.utils.six import StringIO
|
2010-10-10 16:38:28 +00:00
|
|
|
|
2013-10-28 13:17:48 +00:00
|
|
|
test_dir = os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), 'commands'))
|
|
|
|
has_msgfmt = find_command('msgfmt')
|
2010-10-10 16:38:28 +00:00
|
|
|
|
2012-09-07 18:01:46 +00:00
|
|
|
|
2013-10-28 13:17:48 +00:00
|
|
|
@unittest.skipUnless(has_msgfmt, 'msgfmt is mandatory for compilation tests')
|
2013-01-17 03:35:46 +00:00
|
|
|
class MessageCompilationTests(SimpleTestCase):
|
2010-10-10 16:38:28 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self._cwd = os.getcwd()
|
2013-02-27 01:27:35 +00:00
|
|
|
self.addCleanup(os.chdir, self._cwd)
|
|
|
|
os.chdir(test_dir)
|
2010-10-10 16:38:28 +00:00
|
|
|
|
2013-02-27 01:27:35 +00:00
|
|
|
def rmfile(self, filepath):
|
|
|
|
if os.path.exists(filepath):
|
|
|
|
os.remove(filepath)
|
2010-10-10 16:38:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PoFileTests(MessageCompilationTests):
|
|
|
|
|
2012-09-07 18:01:46 +00:00
|
|
|
LOCALE = 'es_AR'
|
|
|
|
MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
2011-12-11 00:07:06 +00:00
|
|
|
|
2010-10-10 16:38:28 +00:00
|
|
|
def test_bom_rejection(self):
|
2012-12-08 10:13:52 +00:00
|
|
|
with self.assertRaises(CommandError) as cm:
|
2013-11-23 02:47:04 +00:00
|
|
|
call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
|
2012-12-08 10:13:52 +00:00
|
|
|
self.assertIn("file has a BOM (Byte Order Mark)", cm.exception.args[0])
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE))
|
2011-12-11 00:07:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PoFileContentsTests(MessageCompilationTests):
|
|
|
|
# Ticket #11240
|
|
|
|
|
2013-10-23 10:09:29 +00:00
|
|
|
LOCALE = 'fr'
|
|
|
|
MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
2011-12-11 00:07:06 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(PoFileContentsTests, self).setUp()
|
|
|
|
self.addCleanup(os.unlink, os.path.join(test_dir, self.MO_FILE))
|
|
|
|
|
|
|
|
def test_percent_symbol_in_po_file(self):
|
2013-11-23 02:47:04 +00:00
|
|
|
call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
|
2011-12-11 00:07:06 +00:00
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE))
|
|
|
|
|
|
|
|
|
|
|
|
class PercentRenderingTests(MessageCompilationTests):
|
|
|
|
# Ticket #11240 -- Testing rendering doesn't belong here but we are trying
|
|
|
|
# to keep tests for all the stack together
|
|
|
|
|
2013-10-23 10:09:29 +00:00
|
|
|
LOCALE = 'it'
|
|
|
|
MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
2011-12-11 00:07:06 +00:00
|
|
|
|
2013-02-27 01:45:08 +00:00
|
|
|
def setUp(self):
|
|
|
|
super(PercentRenderingTests, self).setUp()
|
|
|
|
self.addCleanup(os.unlink, os.path.join(test_dir, self.MO_FILE))
|
|
|
|
|
2011-12-11 00:07:06 +00:00
|
|
|
@override_settings(LOCALE_PATHS=(os.path.join(test_dir, 'locale'),))
|
|
|
|
def test_percent_symbol_escaping(self):
|
|
|
|
from django.template import Template, Context
|
2013-11-23 02:47:04 +00:00
|
|
|
call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
|
2011-12-11 00:07:06 +00:00
|
|
|
with translation.override(self.LOCALE):
|
|
|
|
t = Template('{% load i18n %}{% trans "Looks like a str fmt spec %% o but shouldn\'t be interpreted as such" %}')
|
|
|
|
rendered = t.render(Context({}))
|
|
|
|
self.assertEqual(rendered, 'IT translation contains %% for the above string')
|
|
|
|
|
|
|
|
t = Template('{% load i18n %}{% trans "Completed 50%% of all the tasks" %}')
|
|
|
|
rendered = t.render(Context({}))
|
|
|
|
self.assertEqual(rendered, 'IT translation of Completed 50%% of all the tasks')
|
2012-06-07 09:23:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@override_settings(LOCALE_PATHS=(os.path.join(test_dir, 'locale'),))
|
|
|
|
class MultipleLocaleCompilationTests(MessageCompilationTests):
|
|
|
|
MO_FILE_HR = None
|
|
|
|
MO_FILE_FR = None
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(MultipleLocaleCompilationTests, self).setUp()
|
2013-02-27 01:27:35 +00:00
|
|
|
localedir = os.path.join(test_dir, 'locale')
|
|
|
|
self.MO_FILE_HR = os.path.join(localedir, 'hr/LC_MESSAGES/django.mo')
|
|
|
|
self.MO_FILE_FR = os.path.join(localedir, 'fr/LC_MESSAGES/django.mo')
|
|
|
|
self.addCleanup(self.rmfile, os.path.join(localedir, self.MO_FILE_HR))
|
|
|
|
self.addCleanup(self.rmfile, os.path.join(localedir, self.MO_FILE_FR))
|
2012-06-07 09:23:25 +00:00
|
|
|
|
|
|
|
def test_one_locale(self):
|
2013-11-23 02:47:04 +00:00
|
|
|
call_command('compilemessages', locale=['hr'], stdout=StringIO())
|
2012-06-07 09:23:25 +00:00
|
|
|
|
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE_HR))
|
|
|
|
|
|
|
|
def test_multiple_locales(self):
|
2013-03-16 18:48:40 +00:00
|
|
|
call_command('compilemessages', locale=['hr', 'fr'], stdout=StringIO())
|
2012-06-07 09:23:25 +00:00
|
|
|
|
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE_HR))
|
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE_FR))
|
2013-02-12 16:58:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CompilationErrorHandling(MessageCompilationTests):
|
|
|
|
|
2013-10-23 10:09:29 +00:00
|
|
|
LOCALE = 'ja'
|
|
|
|
MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
2013-02-12 16:58:49 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(CompilationErrorHandling, self).setUp()
|
2013-02-27 01:27:35 +00:00
|
|
|
self.addCleanup(self.rmfile, os.path.join(test_dir, self.MO_FILE))
|
2013-02-12 16:58:49 +00:00
|
|
|
|
|
|
|
def test_error_reported_by_msgfmt(self):
|
|
|
|
with self.assertRaises(CommandError):
|
2013-11-23 02:47:04 +00:00
|
|
|
call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
|