Index: translate/convert/pot2po.py =================================================================== --- translate/convert/pot2po.py (revision 7818) +++ translate/convert/pot2po.py (working copy) @@ -45,16 +45,19 @@ tmmatcher = match.matcher(tmstore, max_candidates=max_candidates, min_similarity=min_similarity, max_length=max_length) return tmmatcher -def convertpot(inputpotfile, outputpofile, templatepofile, tm=None, min_similarity=75, fuzzymatching=True, **kwargs): +def convertpot(inputpotfile, outputpofile, templatepofile, tm=None, min_similarity=75, + fuzzymatching=True, ignore_previous=False, **kwargs): inputpot = po.pofile(inputpotfile) templatepo = None if templatepofile is not None: templatepo = po.pofile(templatepofile) - outputpo = convertpot_stores(inputpot, templatepo, tm, min_similarity, fuzzymatching, **kwargs) + outputpo = convertpot_stores(inputpot, templatepo, tm, min_similarity, + fuzzymatching, ignore_previous, **kwargs) outputpofile.write(str(outputpo)) return 1 -def convertpot_stores(inputpot, templatepo, tm=None, min_similarity=75, fuzzymatching=True, **kwargs): +def convertpot_stores(inputpot, templatepo, tm=None, min_similarity=75, + fuzzymatching=True, ignore_previous=False, **kwargs): """reads in inputpotfile, adjusts header, writes to outputpofile. if templatepofile exists, merge translations from it into outputpofile""" inputpot.makeindex() thetargetfile = po.pofile() @@ -155,8 +158,12 @@ if fuzzyfilematcher: fuzzycandidates = fuzzyfilematcher.matches(inputpotunit.source) if fuzzycandidates: - inputpotunit.merge(fuzzycandidates[0]) original = templatepo.findunit(fuzzycandidates[0].source) + if original and not ignore_previous: + inputpotunit.set_as_previous() + inputpotunit.source = original.source + else: + inputpotunit.merge(fuzzycandidates[0]) if original: original.reused = True if fuzzyglobalmatcher and not fuzzycandidates: @@ -206,6 +213,10 @@ parser.add_option("--nofuzzymatching", dest="fuzzymatching", action="store_false", default=True, help="Disable fuzzy matching") parser.passthrough.append("fuzzymatching") + parser.add_option("-g", "--ignore-previous", dest="ignore_previous", + action="store_true", default=False, + help="Don't keep previous messages") + parser.passthrough.append("ignore_previous") parser.run(argv) Index: translate/convert/test_pot2po.py =================================================================== --- translate/convert/test_pot2po.py (revision 7818) +++ translate/convert/test_pot2po.py (working copy) @@ -13,7 +13,7 @@ def teardown_method(self, method): warnings.resetwarnings() - def convertpot(self, potsource, posource=None): + def convertpot(self, potsource, posource=None, *args, **kwargs): """helper that converts pot source to po source without requiring files""" potfile = wStringIO.StringIO(potsource) if posource: @@ -21,7 +21,9 @@ else: pofile = None pooutfile = wStringIO.StringIO() - pot2po.convertpot(potfile, pooutfile, pofile) + if 'ignore_previous' not in kwargs.keys(): + kwargs['ignore_previous'] = True + pot2po.convertpot(potfile, pooutfile, pofile, *args, **kwargs) pooutfile.seek(0) return po.pofile(pooutfile.read()) @@ -411,6 +413,110 @@ assert newpounit.isfuzzy() assert newpounit.hastypecomment("c-format") + def test_previous_message_without_msgstr(self): + potsource = '#: a.c:1\nmsgid "%d units"\nmsgstr ""\n' + posource = '#: a.c:1\nmsgid "%s units"\nmsgstr ""\n' + poexpected = potsource + newpo = self.convertpot(potsource, posource, ignore_previous=False) + newpounit = self.singleunit(newpo) + assert str(newpounit) == poexpected + assert not newpounit.isfuzzy() + + def test_previous_message_singular(self): + potsource = '#: a.c:1\nmsgid "%d unit"\nmsgstr "%d unidade"\n' + posource = '#: a.c:1\nmsgid "%s unit"\nmsgstr "%s unidade"\n' + poexpected = '''#: a.c:1\n#, fuzzy\n#| msgid "%d unit"\nmsgid "%s unit" +msgstr "%d unidade"\n''' + newpo = self.convertpot(potsource, posource, ignore_previous=False) + newpounit = self.singleunit(newpo) + assert str(newpounit) == poexpected + assert newpounit.isfuzzy() + + def test_previous_message_with_plural(self): + potsource = '''#: a.c:1\nmsgid "%d unit"\nmsgid_plural "%d units" +msgstr[0] "%d unidade"\nmsgstr[1] "%d unidades"''' + posource = '''#: a.c:1\nmsgid "%s unit"\nmsgid_plural "%s units" +msgstr[0] "%s unidade"\nmsgstr[1] "%s unidades"''' + poexpected = '''#: a.c:1 +#, fuzzy +#| msgid "%d unit" +#| msgid_plural "%d units" +msgid "%s unit" +msgid_plural "%s units" +msgstr[0] "%d unidade" +msgstr[1] "%d unidades"\n''' + newpo = self.convertpot(potsource, posource, ignore_previous=False) + newpounit = self.singleunit(newpo) + assert str(newpounit) == poexpected + assert newpounit.isfuzzy() + + def test_previous_message_with_only_pot_plural(self): + potsource = '''#: a.c:1\nmsgid "plural"\nmsgid_plural "plurals" +msgstr[0] "plural"\nmsgstr[1] "plurais"''' + posource = '#: a.c:1\nmsgid "plural"\nmsgstr "plural"\n' + poexpected = '''#: a.c:1 +#, fuzzy +#| msgid "plural" +#| msgid_plural "plurals" +msgid "plural" +msgstr "plural"\n''' + newpo = self.convertpot(potsource, posource, ignore_previous=False) + newpounit = self.singleunit(newpo) + print str(newpounit) + print 'Expected' + print poexpected + assert str(newpounit) == poexpected + assert newpounit.isfuzzy() + + def test_previous_message_with_only_po_plural(self): + #XXX: broken test - missing msgstr[1] + # in msgmerge it just copy the msgstr[0] to msgstr[1] (?) + potsource = '#: a.c:1\nmsgid "unit"\n msgstr "unidade"\n' + posource = '''#: a.c:1\nmsgid "unit"\nmsgid_plural "units" +msgstr[0] "unidade"\nmsgstr[1] "unidades"''' + poexpected = '''#: a.c:1 +#, fuzzy +#| msgid "unit" +msgid "unit" +msgid_plural "units" +msgstr[0] "unidade" +msgstr[1] "unidades"\n''' + newpo = self.convertpot(potsource, posource, ignore_previous=False) + newpounit = self.singleunit(newpo) + #XXX: remove this later + print str(newpounit) + print 'Expected' + print poexpected + assert str(newpounit) == poexpected + assert newpounit.isfuzzy() + + def test_previous_message_with_fuzzy_state_set(self): + potsource = '#: a.c:1\n#, fuzzy\nmsgid "%d units"\nmsgstr "%d unidades"\n' + posource = '#: a.c:1\nmsgid "%s units"\nmsgstr "%s unidades"\n' + poexpected = '#: a.c:1\n#, fuzzy\nmsgid "%s units"\nmsgstr "%d unidades"\n' + newpo = self.convertpot(potsource, posource, ignore_previous=False) + newpounit = self.singleunit(newpo) + assert str(newpounit) == poexpected + assert newpounit.isfuzzy() + + def test_previous_message_with_context(self): + potsource = '''#: a.c:1\nmsgctxt "Testing"\nmsgid "%d unit" +msgstr "%d unidade"''' + posource = '''#: a.c:1\nmsgctxt "Testing"\nmsgid "%s unit" +msgstr "%s unidade"''' + poexpected = '''#: a.c:1 +#, fuzzy +#| msgctxt "Testing" +#| msgid "%d unit" +msgctxt "Testing" +msgid "%s unit" +msgstr "%d unidade"\n''' + newpo = self.convertpot(potsource, posource, ignore_previous=False) + newpounit = self.singleunit(newpo) + assert str(newpounit) == poexpected + assert newpounit.isfuzzy() + + class TestPOT2POCommand(test_convert.TestConvertCommand, TestPOT2PO): """Tests running actual pot2po commands on files""" convertmodule = pot2po @@ -422,5 +528,6 @@ options = self.help_check(options, "-P, --pot") options = self.help_check(options, "--tm") options = self.help_check(options, "-s MIN_SIMILARITY, --similarity=MIN_SIMILARITY") - options = self.help_check(options, "--nofuzzymatching", last=True) + options = self.help_check(options, "--nofuzzymatching") + options = self.help_check(options, "--ignore-previous", last=True)