fgmeta/python3-flightgear/fg-merge-xliff-into-xliff
Florent Rougon 7142621966 i18n Python scripts: add script fg-merge-xliff-into-xliff
This script is designed for the following use case:

Suppose a translator has been working on a particular translation file,
and meanwhile the official XLIFF file for this translation has been
updated in FGData (new translatable strings added, obsolete strings
marked or removed, etc.). In such a case, 'fg-merge-xliff-into-xliff'
can be used to merge the translator's work into the official XLIFF
translation file. Essentially, this means that for all strings that have
the same source text, plural status, number of plural forms and of
course target language, the target texts, "approved" status and
translator comments will be taken from the first file passed in the
following command:

  fg-merge-xliff-into-xliff TRANSLATOR_FILE PROJECT_FILE

Used like this, PROJECT_FILE will be updated with data from
TRANSLATOR_FILE. If you don't want to modify PROJECT_FILE, use the -o
(--output) option. If '-' is passed as argument to this option, then the
result is written to the standard output.
2017-09-19 22:11:41 +02:00

124 lines
4.6 KiB
Python
Executable File

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# fg-merge-xliff-into-xliff --- Merge translations from one XLIFF file into
# another one
# Copyright (C) 2017 Florent Rougon
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import argparse
import locale
import os
import sys
import flightgear.meta.logging
import flightgear.meta.i18n as fg_i18n
PROGNAME = os.path.basename(sys.argv[0])
# Only messages with severity >= info will be printed to the terminal (it's
# possible to also log all messages to a file regardless of their level, see
# the Logger class). Of course, there is also the standard logging module...
logger = flightgear.meta.logging.Logger(
progname=PROGNAME,
logLevel=flightgear.meta.logging.LogLevel.info,
defaultOutputStream=sys.stderr)
def processCommandLine():
params = argparse.Namespace()
parser = argparse.ArgumentParser(
usage="""\
%(prog)s [OPTION ...] SOURCE INTO
Merge strings from a FlightGear XLIFF localization file into another one.""",
description="""\
This program merges a FlightGear XLIFF localization file into another one.
This means that every translatable string that:
(1) exists in both SOURCE and INTO;
(2) has the same target language, source text, plural status and number of
plural forms in SOURCE and in INTO;
is updated from SOURCE, i.e.: the target texts, 'approved' status and
translator comments are copied from SOURCE.
The result is written to INTO unless the -o (--output) option is given.
Note that this program is different from fg-update-translation-files's
'merge-new-master' command, which is for updating an XLIFF file according to
the default translation ("master").
Expected use case: suppose that a translator is working on a translation
file, and meanwhile the official XLIFF file for this translation is updated
in the project repository (new translatable strings added, obsolete strings
marked or removed, etc.). This program can then be used to merge the
translator work into the project file for all strings for which it makes
sense (source text unchanged, same plural status, etc.).""",
formatter_class=argparse.RawDescriptionHelpFormatter,
# I want --help but not -h (it might be useful for something else)
add_help=False)
parser.add_argument("source", metavar="SOURCE",
help="""\
input XLIFF file; read updated translated strings
from this file""")
parser.add_argument("into", metavar="INTO",
help="""\
XLIFF file to compare to SOURCE in order to decide
which translated strings to update; unless the -o
option is used, updated strings are written to this
file""")
parser.add_argument("-o", "--output",
help="""\
write the XLIFF merged output to OUTPUT instead of
INTO. When this option is used, INTO is read but not
modified. If OUTPUT is '-', write the XLIFF merged
output to the standard output.""")
parser.add_argument("--help", action="help",
help="display this message and exit")
return parser.parse_args(namespace=params)
def mergeXliffIntoXliff(source, into, output):
formatHandler = fg_i18n.XliffFormatHandler()
srcTransl = formatHandler.readTranslation(source)
transl = formatHandler.readTranslation(into)
# Merge 'srcTransl' into 'transl'
transl.mergeNonMasterTransl(srcTransl, logger=logger)
# File path, or '-' for the standard output
outputFile = into if output is None else output
formatHandler.writeTranslation(transl, outputFile)
def main():
global params
locale.setlocale(locale.LC_ALL, '')
params = processCommandLine()
mergeXliffIntoXliff(params.source, params.into, params.output)
sys.exit(0)
if __name__ == "__main__": main()