2014-11-16 06:56:37 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
import gettext as gettext_module
|
2010-10-11 00:38:28 +08:00
|
|
|
import os
|
2014-03-24 21:03:06 +08:00
|
|
|
import shutil
|
2014-01-05 05:26:24 +08:00
|
|
|
import stat
|
2013-10-28 21:17:48 +08:00
|
|
|
import unittest
|
2010-10-11 00:38:28 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.management import (
|
|
|
|
CommandError, call_command, execute_from_command_line,
|
|
|
|
)
|
2013-10-28 21:17:48 +08:00
|
|
|
from django.core.management.utils import find_command
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2014-11-29 06:47:53 +08:00
|
|
|
from django.test.utils import captured_stderr, captured_stdout
|
2013-01-17 11:35:46 +08:00
|
|
|
from django.utils import translation
|
2012-12-08 18:13:52 +08:00
|
|
|
from django.utils._os import upath
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.encoding import force_text
|
2012-08-09 05:40:20 +08:00
|
|
|
from django.utils.six import StringIO
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.translation import ugettext
|
2010-10-11 00:38:28 +08:00
|
|
|
|
2013-10-28 21:17:48 +08:00
|
|
|
has_msgfmt = find_command('msgfmt')
|
2010-10-11 00:38:28 +08:00
|
|
|
|
2012-09-08 02:01:46 +08:00
|
|
|
|
2013-10-28 21:17:48 +08:00
|
|
|
@unittest.skipUnless(has_msgfmt, 'msgfmt is mandatory for compilation tests')
|
2013-01-17 11:35:46 +08:00
|
|
|
class MessageCompilationTests(SimpleTestCase):
|
2010-10-11 00:38:28 +08:00
|
|
|
|
2014-03-24 21:03:06 +08:00
|
|
|
test_dir = os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), 'commands'))
|
|
|
|
|
2010-10-11 00:38:28 +08:00
|
|
|
def setUp(self):
|
|
|
|
self._cwd = os.getcwd()
|
2013-02-27 09:27:35 +08:00
|
|
|
self.addCleanup(os.chdir, self._cwd)
|
2014-03-24 21:03:06 +08:00
|
|
|
os.chdir(self.test_dir)
|
|
|
|
|
|
|
|
def _rmrf(self, dname):
|
|
|
|
if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
|
|
|
|
return
|
|
|
|
shutil.rmtree(dname)
|
2010-10-11 00:38:28 +08:00
|
|
|
|
2013-02-27 09:27:35 +08:00
|
|
|
def rmfile(self, filepath):
|
|
|
|
if os.path.exists(filepath):
|
|
|
|
os.remove(filepath)
|
2010-10-11 00:38:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
class PoFileTests(MessageCompilationTests):
|
|
|
|
|
2012-09-08 02:01:46 +08:00
|
|
|
LOCALE = 'es_AR'
|
|
|
|
MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
2011-12-11 08:07:06 +08:00
|
|
|
|
2010-10-11 00:38:28 +08:00
|
|
|
def test_bom_rejection(self):
|
2012-12-08 18:13:52 +08:00
|
|
|
with self.assertRaises(CommandError) as cm:
|
2013-11-23 10:47:04 +08:00
|
|
|
call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
|
2012-12-08 18:13:52 +08:00
|
|
|
self.assertIn("file has a BOM (Byte Order Mark)", cm.exception.args[0])
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE))
|
2011-12-11 08:07:06 +08:00
|
|
|
|
2014-01-05 05:26:24 +08:00
|
|
|
def test_no_write_access(self):
|
|
|
|
mo_file_en = 'locale/en/LC_MESSAGES/django.mo'
|
|
|
|
err_buffer = StringIO()
|
|
|
|
# put file in read-only mode
|
|
|
|
old_mode = os.stat(mo_file_en).st_mode
|
|
|
|
os.chmod(mo_file_en, stat.S_IREAD)
|
|
|
|
try:
|
|
|
|
call_command('compilemessages', locale=['en'], stderr=err_buffer, verbosity=0)
|
|
|
|
err = err_buffer.getvalue()
|
|
|
|
self.assertIn("not writable location", err)
|
|
|
|
finally:
|
|
|
|
os.chmod(mo_file_en, old_mode)
|
|
|
|
|
2011-12-11 08:07:06 +08:00
|
|
|
|
|
|
|
class PoFileContentsTests(MessageCompilationTests):
|
|
|
|
# Ticket #11240
|
|
|
|
|
2013-10-23 18:09:29 +08:00
|
|
|
LOCALE = 'fr'
|
|
|
|
MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
2011-12-11 08:07:06 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(PoFileContentsTests, self).setUp()
|
2014-03-24 21:03:06 +08:00
|
|
|
self.addCleanup(os.unlink, os.path.join(self.test_dir, self.MO_FILE))
|
2011-12-11 08:07:06 +08:00
|
|
|
|
|
|
|
def test_percent_symbol_in_po_file(self):
|
2013-11-23 10:47:04 +08:00
|
|
|
call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
|
2011-12-11 08:07:06 +08: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 18:09:29 +08:00
|
|
|
LOCALE = 'it'
|
|
|
|
MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
2011-12-11 08:07:06 +08:00
|
|
|
|
2013-02-27 09:45:08 +08:00
|
|
|
def setUp(self):
|
|
|
|
super(PercentRenderingTests, self).setUp()
|
2014-03-24 21:03:06 +08:00
|
|
|
self.addCleanup(os.unlink, os.path.join(self.test_dir, self.MO_FILE))
|
2013-02-27 09:45:08 +08:00
|
|
|
|
2011-12-11 08:07:06 +08:00
|
|
|
def test_percent_symbol_escaping(self):
|
2015-01-22 00:55:57 +08:00
|
|
|
with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, 'locale')]):
|
2014-03-24 21:03:06 +08:00
|
|
|
from django.template import Template, Context
|
|
|
|
call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
|
|
|
|
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')
|
2011-12-11 08:07:06 +08:00
|
|
|
|
2014-03-24 21:03:06 +08:00
|
|
|
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 17:23:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
class MultipleLocaleCompilationTests(MessageCompilationTests):
|
2014-03-24 21:03:06 +08:00
|
|
|
|
2012-06-07 17:23:25 +08:00
|
|
|
MO_FILE_HR = None
|
|
|
|
MO_FILE_FR = None
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(MultipleLocaleCompilationTests, self).setUp()
|
2014-03-24 21:03:06 +08:00
|
|
|
localedir = os.path.join(self.test_dir, 'locale')
|
2013-02-27 09:27:35 +08:00
|
|
|
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 17:23:25 +08:00
|
|
|
|
|
|
|
def test_one_locale(self):
|
2015-01-22 00:55:57 +08:00
|
|
|
with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, 'locale')]):
|
2014-03-24 21:03:06 +08:00
|
|
|
call_command('compilemessages', locale=['hr'], stdout=StringIO())
|
2012-06-07 17:23:25 +08:00
|
|
|
|
2014-03-24 21:03:06 +08:00
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE_HR))
|
2012-06-07 17:23:25 +08:00
|
|
|
|
|
|
|
def test_multiple_locales(self):
|
2015-01-22 00:55:57 +08:00
|
|
|
with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, 'locale')]):
|
2014-03-24 21:03:06 +08:00
|
|
|
call_command('compilemessages', locale=['hr', 'fr'], stdout=StringIO())
|
|
|
|
|
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE_HR))
|
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE_FR))
|
|
|
|
|
|
|
|
|
|
|
|
class ExcludedLocaleCompilationTests(MessageCompilationTests):
|
|
|
|
|
|
|
|
test_dir = os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), 'exclude'))
|
|
|
|
|
|
|
|
MO_FILE = 'locale/%s/LC_MESSAGES/django.mo'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(ExcludedLocaleCompilationTests, self).setUp()
|
|
|
|
|
|
|
|
shutil.copytree('canned_locale', 'locale')
|
|
|
|
self.addCleanup(self._rmrf, os.path.join(self.test_dir, 'locale'))
|
|
|
|
|
2014-05-01 15:03:24 +08:00
|
|
|
def test_command_help(self):
|
2014-11-29 06:47:53 +08:00
|
|
|
with captured_stdout(), captured_stderr():
|
2014-05-01 15:03:24 +08:00
|
|
|
# `call_command` bypasses the parser; by calling
|
|
|
|
# `execute_from_command_line` with the help subcommand we
|
|
|
|
# ensure that there are no issues with the parser itself.
|
|
|
|
execute_from_command_line(['django-admin', 'help', 'compilemessages'])
|
|
|
|
|
2014-03-24 21:03:06 +08:00
|
|
|
def test_one_locale_excluded(self):
|
|
|
|
call_command('compilemessages', exclude=['it'], stdout=StringIO())
|
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE % 'en'))
|
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE % 'fr'))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % 'it'))
|
|
|
|
|
|
|
|
def test_multiple_locales_excluded(self):
|
|
|
|
call_command('compilemessages', exclude=['it', 'fr'], stdout=StringIO())
|
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE % 'en'))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % 'fr'))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % 'it'))
|
|
|
|
|
|
|
|
def test_one_locale_excluded_with_locale(self):
|
|
|
|
call_command('compilemessages', locale=['en', 'fr'], exclude=['fr'], stdout=StringIO())
|
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE % 'en'))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % 'fr'))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % 'it'))
|
2012-06-07 17:23:25 +08:00
|
|
|
|
2014-03-24 21:03:06 +08:00
|
|
|
def test_multiple_locales_excluded_with_locale(self):
|
|
|
|
call_command('compilemessages', locale=['en', 'fr', 'it'], exclude=['fr', 'it'],
|
|
|
|
stdout=StringIO())
|
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE % 'en'))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % 'fr'))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % 'it'))
|
2013-02-13 00:58:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CompilationErrorHandling(MessageCompilationTests):
|
|
|
|
|
2013-10-23 18:09:29 +08:00
|
|
|
LOCALE = 'ja'
|
|
|
|
MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
2013-02-13 00:58:49 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(CompilationErrorHandling, self).setUp()
|
2014-03-24 21:03:06 +08:00
|
|
|
self.addCleanup(self.rmfile, os.path.join(self.test_dir, self.MO_FILE))
|
2013-02-13 00:58:49 +08:00
|
|
|
|
|
|
|
def test_error_reported_by_msgfmt(self):
|
|
|
|
with self.assertRaises(CommandError):
|
2013-11-23 10:47:04 +08:00
|
|
|
call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
|
2014-11-16 06:56:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
class FuzzyTranslationTest(MessageCompilationTests):
|
|
|
|
|
|
|
|
LOCALE = 'ru'
|
|
|
|
MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(FuzzyTranslationTest, self).setUp()
|
|
|
|
self.addCleanup(self.rmfile, os.path.join(self.test_dir, self.MO_FILE))
|
|
|
|
gettext_module._translations = {} # flush cache or test will be useless
|
|
|
|
|
|
|
|
def test_nofuzzy_compiling(self):
|
2015-01-22 00:55:57 +08:00
|
|
|
with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, 'locale')]):
|
2014-11-16 06:56:37 +08:00
|
|
|
call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
|
|
|
|
with translation.override(self.LOCALE):
|
|
|
|
self.assertEqual(ugettext('Lenin'), force_text('Ленин'))
|
|
|
|
self.assertEqual(ugettext('Vodka'), force_text('Vodka'))
|
|
|
|
|
|
|
|
def test_fuzzy_compiling(self):
|
2015-01-22 00:55:57 +08:00
|
|
|
with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, 'locale')]):
|
2014-11-16 06:56:37 +08:00
|
|
|
call_command('compilemessages', locale=[self.LOCALE], fuzzy=True, stdout=StringIO())
|
|
|
|
with translation.override(self.LOCALE):
|
|
|
|
self.assertEqual(ugettext('Lenin'), force_text('Ленин'))
|
|
|
|
self.assertEqual(ugettext('Vodka'), force_text('Водка'))
|