Commit cb77826a by Calen Pennington

Merge pull request #2257 from cpennington/test-compiled-translations

Add tests of compiled translated strings, so that we can check them into the repo safely.
parents 9069e413 d70eb3aa
......@@ -11,7 +11,7 @@ def execute(command, working_directory=BASE_DIR):
Output is ignored.
"""
LOG.info(command)
subprocess.check_output(command.split(' '), cwd=working_directory, stderr=subprocess.STDOUT)
subprocess.check_call(command, cwd=working_directory, stderr=sys.STDOUT, shell=True)
def call(command, working_directory=BASE_DIR):
......
"""
Test that the compiled .mo files match the translations in the
uncompiled .po files.
This is required because we are checking in the .mo files into
the repo, but compiling them is a manual process. We want to make
sure that we find out if someone forgets the compilation step.
"""
import ddt
import polib
from unittest import TestCase
from i18n.config import CONFIGURATION, LOCALE_DIR
@ddt.ddt
class TestCompiledMessages(TestCase):
"""
Test that mo files match their source po files
"""
PO_FILES = ['django.po', 'djangojs.po']
@ddt.data(*CONFIGURATION.locales)
def test_translated_messages(self, locale):
message_dir = LOCALE_DIR / locale / 'LC_MESSAGES'
for pofile_name in self.PO_FILES:
pofile_path = message_dir / pofile_name
pofile = polib.pofile(pofile_path)
mofile = polib.mofile(pofile_path.stripext() + '.mo')
po_entries = {entry.msgid: entry for entry in pofile.translated_entries()}
mo_entries = {entry.msgid: entry for entry in mofile.translated_entries()}
# Check that there are no entries in po that aren't in mo, and vice-versa
self.assertEquals(po_entries.viewkeys(), mo_entries.viewkeys())
for entry_id, po_entry in po_entries.iteritems():
mo_entry = mo_entries[entry_id]
for attr in ('msgstr', 'msgid_plural', 'msgstr_plural', 'msgctxt', 'obsolete', 'encoding'):
po_attr = getattr(po_entry, attr)
mo_attr = getattr(mo_entry, attr)
# The msgstr_plural in the mo_file is keyed on ints, but in the po_file it's
# keyed on strings. This normalizes them.
if attr == 'msgstr_plural':
po_attr = {int(key): val for (key, val) in po_attr.items()}
self.assertEquals(
po_attr,
mo_attr,
"When comparing {} for entry {!r}, {!r} from the .po file doesn't match {!r} from the .mo file".format(
attr,
entry_id,
po_attr,
mo_attr,
)
)
......@@ -7,11 +7,8 @@ namespace :i18n do
sh(File.join(REPO_ROOT, "i18n", "extract.py"))
end
desc "Compile localizable strings from sources. With optional flag 'extract', will extract strings first."
task :generate => "i18n:validate:gettext" do
if ARGV.last.downcase == 'extract'
Rake::Task["i18n:extract"].execute
end
desc "Compile localizable strings from sources, extracting strings first."
task :generate => "i18n:extract" do
sh(File.join(REPO_ROOT, "i18n", "generate.py"))
end
......
......@@ -89,7 +89,7 @@ sphinx==1.1.3
# Used for Internationalization and localization
Babel==1.3
transifex-client==0.9.1
transifex-client==0.10
# Used for testing
coverage==3.7
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment