language: added full translation support
authorFredrik Unger <fred@tree.se>
Tue, 22 Jan 2019 20:15:52 +0000 (21:15 +0100)
committerFredrik Unger <fred@tree.se>
Tue, 22 Jan 2019 20:15:52 +0000 (21:15 +0100)
Adding the support for secondary languages through xlf files.
Using the okapi framwork it is possible to use the tools provided
in that project to produce xlf files and translate them with different
tranlation tools like Machine tranlsation (MT) or Translation Memory (TM).

treecutter/directory.py
treecutter/main.py
treecutter/sitemap.py
treecutter/trie.py

index d4a0973cbd6cb3eee76906a7f6babf56585a5955..88390bb0ef9039a19f98878d2ecf121842edb51e 100644 (file)
@@ -5,15 +5,27 @@ from lxml import etree
 import treecutter.constants as const
 from treecutter.docbook import Docbook
 import re
+from itertools import chain
 
 class Directory():
     """Class containing the state of the directory with articles"""
     def __init__(self):
         self._cwd = u'.'
+        self._translations = []
         self._tree = []
         self._basepath = re.compile('[/\w\._-]*/[\w-]+',re.UNICODE)
 
-    def scan(self):
+    def translations(self, directory):
+        paths = (self._cwd, directory)
+        for dirname, dirnames, filenames in chain.from_iterable(os.walk(path) for path in paths):
+            for filename in filenames:
+                if fnmatch.fnmatch(filename, '*.xlf'):
+                    file_ = os.path.join(dirname,filename)
+                    self._translations.append(file_)
+        return self._translations
+
+
+    def scan(self, draftflag, levelflag):
         for dirname, dirnames, filenames in os.walk(self._cwd):
             for filename in filenames:
                 if fnmatch.fnmatch(filename, '*.xml'):
index c685f7d3756951ea9a123c29461a52b49ad8aa06..c12184d3fd309471f5cecc6345c0ab6fed6c0610 100644 (file)
@@ -4,6 +4,7 @@ from time import time
 import argparse
 from treecutter.directory import Directory
 from treecutter.sitemap import Sitemap
+from treecutter.tools import translate
 
 def main():
 
@@ -22,6 +23,15 @@ def main():
     ts = time()
     print "--= Treecutter =--"
     dir_ = Directory()
+    t1 = time()
+    totrans = dir_.translations(args.style)
+    print "Translate [%d] : [" % (len(totrans)),
+    translate(totrans)
+    print "]"
+    t2 = time()
+    print "Translate[%5.2f s]" % (round(t2-t1,2))
+
+
     sitemap = Sitemap(args)
 
     # Scanning current directory and subdirectory for docbook articles
index 347440b777eb8418158ed90d21ceaa0ac139b4fd..74b01cfbfe1be02344899b8ed1b8caf33d21dc42 100644 (file)
@@ -120,15 +120,16 @@ class Sitemap():
             isoxml = u"//iso_639_3_entry[@*='"+l+"']"
             ln = self._isocode.xpath(isoxml)[0].get('name')
             if lang != 'en':
-                ln = self._tranlang[lang].gettext(ln)
+                ln = self._tranlang[lang].ugettext(ln)
             p = unicode(link.link())
             if p[-1] == u'/':
                 p = p +u'index'
             p = p+u'.'+l
-            li = html.li(html.a(ln.decode('utf-8'),
+            li = html.li(html.a(ln,
                                 href=self._subdir+p,hreflang=l))
             menu.append(li)
-        return etree.tostring(menu,encoding='UTF-8',pretty_print=False)
+#            print type(etree.tostring(menu,encoding='unicode',pretty_print=False))
+        return etree.tostring(menu,encoding='unicode',pretty_print=False)
 
     def publish(self):
         print "Size [ %7s ]" % (sizeof_fmt(get_folder_size(self._tmptarget)))
index 3d3ae2a3f4e842cf014376a387b630b2823b26ae..8c5232e74bf4c5c362c215a3afccccac788981ce 100644 (file)
@@ -71,8 +71,11 @@ class Trie():
                 html += '<li%s><a href="%s%s">%s</a>\n' \
                     % (sel,subdir,l.value().link(),p.menu())
             else:
-                html += '<li%s><a href="%s%s.en" hreflang="en">%s</a>*\n' \
-                    % (sel,subdir,l.value().link(), l.value().page('en').menu())
+                link =l.value().link()
+                if link[-1] == u'/':
+                     link = link +u'index'
+                html += '<li%s><a href="%s%s.en" hreflang="en">%s</a>\n' \
+                    % (sel,subdir,link, l.value().page('en').menu())
             if l.children():
                 html += self._menu(l.children(), lang, page, "", subdir)
             html += "</li>\n"