[gnome-keysign: 10/18] translate appdata using lxml



commit f867b703cdd604b4bf73b09e9fae73a7ed273cdd
Author: RyuzakiKK <aasonykk gmail com>
Date:   Thu Oct 26 14:49:16 2017 +0200

    translate appdata using lxml
    
    lxml is advertised as "mostly compatible but superior to the well-known
    ElementTree API". In fact it allows us to directly access the parent of
    an element, to indent the xml with a parameter and to get the line
    number.

 ...n.appdata.xml => gnome-keysign.raw.appdata.xml} | 22 ++++++++--------
 setup.py                                           | 30 ++++++++++++++++++++++
 2 files changed, 41 insertions(+), 11 deletions(-)
---
diff --git a/data/gnome-keysign.appdata.xml b/data/gnome-keysign.raw.appdata.xml
similarity index 86%
rename from data/gnome-keysign.appdata.xml
rename to data/gnome-keysign.raw.appdata.xml
index 60dcf40..6465e88 100644
--- a/data/gnome-keysign.appdata.xml
+++ b/data/gnome-keysign.raw.appdata.xml
@@ -7,17 +7,17 @@
   <summary>OpenPGP Keysigning helper</summary>
   
   <description>
-   <p>
-     GNOME-Keysign allows signing OpenPGP keys comfortably and securely
-     via the local network or Bluetooth.
-   </p>
-   <p>
-    It can scan another key's barcode and transfer the key securely,
-    allowing for casual two-party key signing sessions.
-    It follows best practises by sending the encrypted signatures
-    to the UIDs of a key using the Email client the user configured
-    to use.
-   </p>
+    <p translatable="yes">
+      GNOME-Keysign allows signing OpenPGP keys comfortably and securely
+      via the local network or Bluetooth.
+    </p>
+    <p translatable="yes">
+      It can scan another key's barcode and transfer the key securely,
+      allowing for casual two-party key signing sessions.
+      It follows best practises by sending the encrypted signatures
+      to the UIDs of a key using the Email client the user configured
+      to use.
+    </p>
   </description>
   
 ​  <launchable type="desktop-id">org.gnome.Keysign.desktop</launchable>
diff --git a/setup.py b/setup.py
index a34edb3..827d10f 100644
--- a/setup.py
+++ b/setup.py
@@ -68,6 +68,33 @@ def translate_desktop_file(infile, outfile, localedir):
         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', pretty_print=True)
 
 
 def get_catalogs(localedir):
@@ -101,6 +128,7 @@ class BuildWithCompile(build):
 
     def run(self):
         translate_desktop_file('data/gnome-keysign.raw.desktop', 'data/gnome-keysign.desktop', 
'keysign/locale')
+        translate_appdata_file('data/gnome-keysign.raw.appdata.xml', 'data/gnome-keysign.appdata.xml', 
'keysign/locale')
         build.run(self)
 
 
@@ -205,6 +233,7 @@ setup(
     },
     setup_requires=[
         "babel",
+        "lxml",
     ],
     tests_require=[
         "pgpy",
@@ -260,6 +289,7 @@ setup(
         message_extractors = {
             '': [
                ('**.raw.desktop', 'babelglade:extract_desktop', None),
+               ('**.raw.appdata.xml', 'babelglade:extract_glade', None),
             ],
             'keysign': [
                 ('**.py', 'python', None),


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