[gnome-keysign: 1/22] setup: use BabelGladeExtractor from pip



commit 4602960ee9e81982d0e4d7fd70b5cc93cdfcccb9
Author: Tobias Mueller <muelli cryptobitch de>
Date:   Mon May 20 09:10:51 2019 +0200

    setup: use BabelGladeExtractor from pip
    
    We have refactored the code into its own module which will hopefully be
    helpful to somebody else.

 babelglade/LICENSE     | 28 ---------------
 babelglade/__init__.py | 50 --------------------------
 setup.py               | 95 ++------------------------------------------------
 3 files changed, 2 insertions(+), 171 deletions(-)
---
diff --git a/setup.py b/setup.py
index 91d1763..662117b 100644
--- a/setup.py
+++ b/setup.py
@@ -28,105 +28,13 @@ with open(os.path.join('keysign', '_version.py')) as f:
     exec(f.read())
 
 
-def translate_desktop_file(infile, outfile, localedir):
-    infp = codecs.open(infile, 'rb', encoding='utf-8')
-    outfp = codecs.open(outfile, 'wb', encoding='utf-8')
-
-    catalogs = get_catalogs(localedir)
-
-    for line in (x.strip() for x in infp):
-        logging.debug('Found in original (%s): %r', type(line), line)
-        # We intend to ignore the first line
-        if line.startswith('[Desktop'):
-            additional_lines = []
-        else:
-            additional_lines = []
-            # This is a rather primitive approach to generating the translated
-            # desktop file.  For example we don't really care about all the
-            # keys in the file.  But its simplicity is a feature and we
-            # ignore the runtime overhead, because it should only run centrally
-            # once.
-            key, value = line.split('=', 1)
-            logging.debug("Found key: %r", key)
-            for locale, catalog in catalogs.items():
-                translated = catalog.get(value)
-                logging.debug("Translated %r[%r]=%r: %r (%r)",
-                    key, locale, value, translated,
-                    translated.string if translated else '')
-                if translated and translated.string \
-                              and translated.string != value:
-                    additional_line = u'{keyword}[{locale}]={translated}'.format(
-                                        keyword=key,
-                                        locale=locale,
-                                        translated=translated.string,
-                                    )
-                    additional_lines.append(additional_line)
-                logging.debug("Writing more lines: %s", additional_lines)
-
-        # Write the new file.
-        # First the original line found it in the file, then the translations.
-        outfp.writelines((outline+'\n' for outline in ([line] + additional_lines)))
-
-
-def translate_appdata_file(infile, outfile, localedir):
-    from lxml import etree
-    catalogs = get_catalogs(localedir)
-    parser = etree.XMLParser(remove_blank_text=True)
-    tree = etree.parse(infile, parser)
-    root = tree.getroot()
-    for elem in root.iter():
-        # We remove any possible tailing whitespaces to allow lxml to format the output
-        elem.tail = None
-        if elem.get("translatable") == "yes":
-            elem.attrib.pop("translatable", None)
-            elem.attrib.pop("comments", None)  # Are comments allowed?
-            message = elem.text
-            parent = elem.getparent()
-            pos = parent.getchildren().index(elem) + 1
-            for locale, catalog in catalogs.items():
-                translated = catalog.get(message)
-                if translated and translated.string \
-                        and translated.string != message:
-                    logging.debug("Translated [%s]%r: %r (%r)",
-                                  locale, message, translated, translated.string)
-                    tr = etree.Element(elem.tag)
-                    attrib = tr.attrib
-                    attrib["{http://www.w3.org/XML/1998/namespace}lang";] = str(locale)
-                    tr.text = translated.string
-                    parent.insert(pos, tr)
-    tree.write(outfile, encoding='utf-8', xml_declaration=True, pretty_print=True)
-
-
-def get_catalogs(localedir):
-    # We import it here rather than globally because
-    # we don't have a guarantee for babel to be available
-    # globally. The setup_requires can only be evaluated after
-    # this file has been loaded. And it can't load if the import
-    # cannot be resolved.
-    from babel.messages.pofile import read_po
-
-    # glob in Python 3.5 takes ** syntax
-    # pofiles = glob.glob(os.path.join(localedir, '**.po', recursive=True))
-    pofiles = sorted([os.path.join(dirpath, f)
-               for dirpath, dirnames, files in os.walk(localedir)
-               for f in files if f.endswith('.po')])
-    logging.debug('Loading %r', pofiles)
-    catalogs = {}
-
-    for pofile in pofiles:
-        catalog = read_po(codecs.open(pofile, 'r', encoding="utf-8"))
-        catalogs[catalog.locale] = catalog
-        logging.info("Found %d strings for %s", len(catalog), catalog.locale)
-        # logging.debug("Strings for %r", catalog, catalog.values())
-    if not catalogs:
-        logging.warning("Could not find pofiles in %r", pofiles)
-    return catalogs
 
 
 class BuildWithCompile(build):
     sub_commands = [('compile_catalog', None)] + build.sub_commands
 
     def run(self):
+        # from babelglade import translate_desktop_file
         translate_desktop_file('data/org.gnome.Keysign.raw.desktop', 'data/org.gnome.Keysign.desktop', 
'keysign/locale')
         translate_appdata_file('data/org.gnome.Keysign.raw.appdata.xml', 
'data/org.gnome.Keysign.appdata.xml', 'keysign/locale')
         build.run(self)
@@ -229,6 +137,7 @@ setup(
     setup_requires=[
         "babel",
         "lxml",
+        "BabelGladeExtractor",
     ],
     tests_require=[
         "nose",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]