Kornel Benko wrote:

> Am Freitag, 13. November 2015 um 20:18:46, schrieb Georg Baum
> <georg.b...@post.rwth-aachen.de>
>> 
>> I am tempted to write our own merge tool in python..
>
> I have a script ready, though it is perl.

Nice, but a pity that it is not python;-( Many years ago it was decided that 
all scripts should be python, and everything was converted.

> It has to be adapted to ones local paths. Also one has to specify which
> languages to are to be handled.

I did it a bit different. If you put the attached file into 
development/tools, and run it (the single argument is the po directory you 
want to take the translations from), you'll get this:

Merging ../lyx-2.1-git/po/pt_PT.po into po/pt_PT.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/ja.po into po/ja.po: Updated 7 translations. 
Merging ../lyx-2.1-git/po/sl.po into po/sl.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/bg.po into po/bg.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/fr.po into po/fr.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/id.po into po/id.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/fi.po into po/fi.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/ar.po into po/ar.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/ia.po into po/ia.po: Updated 17 translations.
Merging ../lyx-2.1-git/po/cs.po into po/cs.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/zh_CN.po into po/zh_CN.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/nl.po into po/nl.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/el.po into po/el.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/de.po into po/de.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/en.po into po/en.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/pt_BR.po into po/pt_BR.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/wa.po into po/wa.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/sr.po into po/sr.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/ko.po into po/ko.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/hu.po into po/hu.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/ro.po into po/ro.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/tr.po into po/tr.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/uk.po into po/uk.po: Updated 3 translations. 
Merging ../lyx-2.1-git/po/ru.po into po/ru.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/he.po into po/he.po: Updated 115 translations.
Merging ../lyx-2.1-git/po/sk.po into po/sk.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/da.po into po/da.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/nb.po into po/nb.po: Updated 108 translations.
Merging ../lyx-2.1-git/po/es.po into po/es.po: Updated 3 translations. 
Merging ../lyx-2.1-git/po/pl.po into po/pl.po: Updated 256 translations.
Merging ../lyx-2.1-git/po/eu.po into po/eu.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/gl.po into po/gl.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/ca.po into po/ca.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/sv.po into po/sv.po: Updated 37 translations.
Merging ../lyx-2.1-git/po/nn.po into po/nn.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/it.po into po/it.po: Updated 0 translations. 
Merging ../lyx-2.1-git/po/zh_TW.po into po/zh_TW.po: Updated 0 translations. 

Not bad for less than 50 lines of code and less than one hour of work, is't 
it? Most po files are up to date, but there a few where merging can save a 
lot of work.

Unfortunately the resulting diff is huge because of reformatting. If we 
decide to use this script, I'll improve it to keep the existing formatting.


Georg
#! /usr/bin/env python
# -*- coding: utf-8 -*-

# file updatelayouts.py
# This file is part of LyX, the document processor.
# Licence details can be found in the file COPYING.

# author Georg Baum

# Full author contact details are available in file CREDITS

# This script takes missing translations from another set of po files and
# merges them into the po files in this source tree.


import os, re, string, sys
import polib


def mergepo(target, source):
    if os.path.exists(source):
        sys.stderr.write('Merging %s into %s: ' % (source, target))
        changed = 0
        po1 = polib.pofile(target)
        po2 = polib.pofile(source)
        for entry in po1.untranslated_entries():
            other = po2.find(entry.msgid, include_obsolete_entries=True)
            if not other:
                continue
            if other.translated():
                entry.msgstr = other.msgstr
                changed = changed + 1
        sys.stderr.write('Updated %d translations.\n' % changed)
        if changed > 0:
            po1.save(target)
    else:
        sys.stderr.write('Skipping %s since %s does not exist' % (target, source))


def main(argv):

    toolsdir = os.path.dirname(argv[0])
    podir1 = os.path.normpath(os.path.join(toolsdir, '../../po'))
    if len(argv) <= 1:
        sys.stderr.write('''Usage: %s <dir> where dir is a directory containing the .po files
       you want to take missing translations from.\n''' % (argv[0]))
    podir2 = os.path.abspath(argv[1])
    for i in os.listdir(podir1):
        (base, ext) = os.path.splitext(i)
        if ext != ".po":
            continue
        mergepo(os.path.join(podir1, i), os.path.join(podir2, i))

    return 0


if __name__ == "__main__":
    main(sys.argv)

Reply via email to