diff options
author | Jussi Pakkanen <jpakkane@gmail.com> | 2020-03-20 21:40:19 +0200 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2020-03-31 21:52:38 +0200 |
commit | d2c23609083d7b3e5267b1e4c923476cbc509d00 (patch) | |
tree | 1837efd1dd2b0cca704287d2bd6b473b7a87bfec /solenv/bin/desktop-translate.py | |
parent | bb844eb299b614c1fa56e140630db070cf709a02 (diff) |
tdf#130911: convert desktop-translate from Perl to Python.
Change-Id: Ic0a84f4e46f4bc3312d15a31ea16060851302d2d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/90847
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'solenv/bin/desktop-translate.py')
-rw-r--r-- | solenv/bin/desktop-translate.py | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/solenv/bin/desktop-translate.py b/solenv/bin/desktop-translate.py new file mode 100644 index 000000000000..e0dbc3078082 --- /dev/null +++ b/solenv/bin/desktop-translate.py @@ -0,0 +1,140 @@ +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This file incorporates work covered by the following license notice: +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed +# with this work for additional information regarding copyright +# ownership. The ASF licenses this file to you under the Apache +# License, Version 2.0 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.apache.org/licenses/LICENSE-2.0 . +# + +# +# Translates multiple .desktop files at once with strings from .ulf +# files; if you add new translatable .ulf files please add them to +# l10ntools/source/localize.cxx +# + +import os, sys, argparse + +parser = argparse.ArgumentParser() +parser.add_argument('-p', dest='productname', default='LibreOffice') +parser.add_argument('-d', dest='workdir', default='.') +parser.add_argument('--key', dest='key') +parser.add_argument('--prefix', dest='prefix', default='') +parser.add_argument('--ext', dest='ext') +parser.add_argument('--template-dir', dest='template_dir', default=None) +parser.add_argument('ifile') + +o = parser.parse_args() + +if o.template_dir is None: + template_dir = '{}/{}'.format(o.workdir, o.prefix) +else: + template_dir = o.template_dir + +# hack for unity section +if o.key == "UnityQuickList": + outkey = "Name" +else: + outkey = o.key + + +templates = {} + +# open input file +source = open(o.ifile) + +template = None + +# read ulf file +for line in source: + if line.strip() == '': + continue + if line[0] == "[": + template = line.split(']', 1)[0][1:] + entry = {} + # For every section in the specified ulf file there should exist + # a template file in $workdir .. + entry['outfile'] = "{}{}.{}".format(template_dir, template, o.ext) + entry['translations'] = {} + templates[template] = entry + else: + # split locale = "value" into 2 strings + if ' = ' not in line: + continue + locale, value = line.split(' = ') + + if locale != line: + # replace en-US with en + locale.replace('en-US', 'en') + + # use just anything inside the "" + value = value.strip() + assert(value[0] == '"') + assert(value[-1] == '"') + value = value[1:-1] + + # replace resource placeholder + value = value.replace('%PRODUCTNAME', o.productname) + + locale = locale.replace('-', '_') + + templates[template]['translations'][locale] = value + +source.close() + +processed = 0 +# process templates +for template in templates: + outfilename = templates[template]['outfile'] + + # open the template file - ignore sections for which no + # templates exist + try: + template_file = open(outfilename) + except Exception: + # string files processed one by one + if o.ext == 'str': + continue + sys.exit("Warning: No template found for item '{}' : '{}' : '{}': $!\n".format(template, outfile, line)) + processed += 1 + + # open output file + tmpfilename = '{}.tmp'.format(outfilename) + outfile = open(tmpfilename, 'w') + + # emit the template to the output file + for line in template_file: + keyline = line + if keyline.startswith(o.key): + keyline = keyline[len(o.key):] + outkey + outfile.write(keyline) + if o.key in line: + translations = templates[template]['translations'] + for locale in sorted (translations.keys()): + value = translations.get(locale, None) + # print "locale is $locale\n"; + # print "value is $value\n"; + if value: + if o.ext == "desktop" or o.ext == "str": + outfile.write("""{}[{}]={}\n""".format(outkey, locale, value)) + else: + outfile.write("""\t[{}]{}={}\n""".format(locale, outkey, value)) + + template_file.close() + + outfile.close() + if os.path.exists(outfilename): + os.unlink(outfilename) + os.rename(tmpfilename, outfilename) + +if o.ext == 'str' and processed == 0: + sys.exit("Warning: No matching templates processed") |