From a59a9bb9d3d9aa047f459c615d23b7854c0bab0f Mon Sep 17 00:00:00 2001 From: Will Kahn-Greene Date: Wed, 4 Nov 2015 21:12:47 -0500 Subject: [PATCH] Fix translation o' plurals It was settin' th' plurals in index '0' ratherr than 0. Bleh. Fixes #79 --- dennis/translator.py | 4 ++-- tests/__init__.py | 5 +++++ tests/test_cmdline.py | 32 +++++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/dennis/translator.py b/dennis/translator.py index f5696fd..a2a3191 100644 --- a/dennis/translator.py +++ b/dennis/translator.py @@ -735,9 +735,9 @@ def translate_file(self, fname): count = 0 for entry in po: if entry.msgid_plural: - entry.msgstr_plural['0'] = self.translate_string( + entry.msgstr_plural[0] = self.translate_string( entry.msgid) - entry.msgstr_plural['1'] = self.translate_string( + entry.msgstr_plural[1] = self.translate_string( entry.msgid_plural) else: entry.msgstr = self.translate_string(entry.msgid) diff --git a/tests/__init__.py b/tests/__init__.py index 2e2c0ad..674c77b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -17,3 +17,8 @@ def build_po_string(data): return POFILE_HEADER + '\n' + data + + +def nix_header(pot_file): + """Nix the POT file header since it changes and we don't care""" + return pot_file[pot_file.find('\n\n')+2:] diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index 7f3fa5a..a3d5f61 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -1,8 +1,10 @@ +from textwrap import dedent + from click.testing import CliRunner import pytest from dennis.cmdline import cli -from tests import build_po_string +from tests import build_po_string, nix_header @pytest.fixture @@ -125,6 +127,34 @@ def test_nonexistent_file(self, runner, tmpdir): last_line = result.output.splitlines()[-1] assert last_line == 'Error: file %s does not exist.' % str(fn) + def test_plurals(self, runner, tmpdir): + po_file = build_po_string( + '#: test_project/base/views.py:12 test_project/base/views.py:22\n' + '#, python-format\n' + 'msgid "%(num)s apple"\n' + 'msgid_plural "%(num)s apples"\n' + 'msgstr[0] ""\n' + 'msgstr[1] ""\n' + ) + fn = tmpdir.join('django.po') + fn.write(po_file) + + result = runner.invoke(cli, ('translate', '-p', 'shouty', str(fn))) + assert result.exit_code == 0 + pot_file = nix_header(fn.read()) + # FIXME: This has the wrong variables, too. We need to fix that, too. + assert ( + pot_file == + dedent("""\ + #: test_project/base/views.py:12 test_project/base/views.py:22 + #, python-format + msgid "%(num)s apple" + msgid_plural "%(num)s apples" + msgstr[0] "%(NUM)S APPLE" + msgstr[1] "%(NUM)S APPLES" + """) + ) + class TestLint: def test_help(self, runner):