2010-10-10 16:38:28 +00:00
|
|
|
import os
|
2012-05-05 17:47:03 +00:00
|
|
|
from io import BytesIO
|
2010-10-10 16:38:28 +00:00
|
|
|
|
|
|
|
from django.core.management import CommandError
|
|
|
|
from django.core.management.commands.compilemessages import compile_messages
|
|
|
|
from django.test import TestCase
|
2011-12-11 00:07:06 +00:00
|
|
|
from django.test.utils import override_settings
|
|
|
|
from django.utils import translation
|
2010-10-10 16:38:28 +00:00
|
|
|
|
2011-12-11 00:07:06 +00:00
|
|
|
test_dir = os.path.abspath(os.path.dirname(__file__))
|
2010-10-10 16:38:28 +00:00
|
|
|
|
|
|
|
class MessageCompilationTests(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self._cwd = os.getcwd()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.chdir(self._cwd)
|
|
|
|
|
|
|
|
|
|
|
|
class PoFileTests(MessageCompilationTests):
|
|
|
|
|
2011-12-11 00:07:06 +00:00
|
|
|
LOCALE='es_AR'
|
|
|
|
MO_FILE='locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
|
|
|
|
2010-10-10 16:38:28 +00:00
|
|
|
def test_bom_rejection(self):
|
2011-12-11 00:07:06 +00:00
|
|
|
os.chdir(test_dir)
|
|
|
|
# We don't use the django.core.management infrastructure (call_command()
|
2010-10-10 16:38:28 +00:00
|
|
|
# et al) because CommandError's cause exit(1) there. We test the
|
|
|
|
# underlying compile_messages function instead
|
2012-05-05 17:47:03 +00:00
|
|
|
out = BytesIO()
|
2011-12-11 00:07:06 +00:00
|
|
|
self.assertRaises(CommandError, compile_messages, out, locale=self.LOCALE)
|
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
|
|
|
|
|
|
|
|
LOCALE='fr'
|
|
|
|
MO_FILE='locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
|
|
|
|
|
|
|
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):
|
|
|
|
os.chdir(test_dir)
|
|
|
|
# We don't use the django.core.management infrastructure (call_command()
|
|
|
|
# et al) because CommandError's cause exit(1) there. We test the
|
|
|
|
# underlying compile_messages function instead
|
2012-05-05 17:47:03 +00:00
|
|
|
out = BytesIO()
|
2011-12-11 00:07:06 +00:00
|
|
|
compile_messages(out, locale=self.LOCALE)
|
|
|
|
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
|
|
|
|
|
|
|
|
LOCALE='it'
|
|
|
|
MO_FILE='locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
|
|
|
|
|
|
|
@override_settings(LOCALE_PATHS=(os.path.join(test_dir, 'locale'),))
|
|
|
|
def test_percent_symbol_escaping(self):
|
|
|
|
from django.template import Template, Context
|
|
|
|
os.chdir(test_dir)
|
|
|
|
# We don't use the django.core.management infrastructure (call_command()
|
|
|
|
# et al) because CommandError's cause exit(1) there. We test the
|
|
|
|
# underlying compile_messages function instead
|
2012-05-05 17:47:03 +00:00
|
|
|
out = BytesIO()
|
2011-12-11 00:07:06 +00:00
|
|
|
compile_messages(out, locale=self.LOCALE)
|
|
|
|
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')
|