From 110491feab71206be6fdd0e45e2a9da6fcf0cc61 Mon Sep 17 00:00:00 2001 From: Fredrik Unger Date: Tue, 22 Jan 2019 21:15:52 +0100 Subject: [PATCH] language: added full translation support 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 | 14 +++++++++++++- treecutter/main.py | 10 ++++++++++ treecutter/sitemap.py | 7 ++++--- treecutter/trie.py | 7 +++++-- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/treecutter/directory.py b/treecutter/directory.py index d4a0973..88390bb 100644 --- a/treecutter/directory.py +++ b/treecutter/directory.py @@ -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'): diff --git a/treecutter/main.py b/treecutter/main.py index c685f7d..c12184d 100644 --- a/treecutter/main.py +++ b/treecutter/main.py @@ -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 diff --git a/treecutter/sitemap.py b/treecutter/sitemap.py index 347440b..74b01cf 100644 --- a/treecutter/sitemap.py +++ b/treecutter/sitemap.py @@ -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))) diff --git a/treecutter/trie.py b/treecutter/trie.py index 3d3ae2a..8c5232e 100644 --- a/treecutter/trie.py +++ b/treecutter/trie.py @@ -71,8 +71,11 @@ class Trie(): html += '%s\n' \ % (sel,subdir,l.value().link(),p.menu()) else: - html += '%s*\n' \ - % (sel,subdir,l.value().link(), l.value().page('en').menu()) + link =l.value().link() + if link[-1] == u'/': + link = link +u'index' + html += '%s\n' \ + % (sel,subdir,link, l.value().page('en').menu()) if l.children(): html += self._menu(l.children(), lang, page, "", subdir) html += "\n" -- 2.30.2