Last active
August 10, 2018 14:10
-
-
Save nesterchung/2f2acad6fce7ebc8baff1288633309d7 to your computer and use it in GitHub Desktop.
Revisions
-
nesterchung revised this gist
Aug 10, 2018 . No changes.There are no files selected for viewing
-
nesterchung revised this gist
Aug 10, 2018 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -61,8 +61,8 @@ def translate(filein, fileout): if __name__ == "__main__": PARSER = argparse.ArgumentParser(description='Translate xliff file') PARSER.add_argument('filein', metavar='INPUT', help='input filename') PARSER.add_argument('-o', '--output', dest='fileout' help='output filename') PARSER.add_argument('-v', '--verbose', action='store_true') ARGS = PARSER.parse_args() -
nesterchung revised this gist
Aug 10, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # File : gist:2f2acad6fce7ebc8baff1288633309d7/xliff_translate.py # Author : Nester Chung <twntwn3838@gmail> # Date : 09.08.2018 # Last Modified Date: 10.08.2018 -
nesterchung revised this gist
Aug 10, 2018 . 1 changed file with 10 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,7 +22,16 @@ TRANSLATOR = Translator() #xliff language code mapping to google language code LM = {"zh-Hans" : "zh-Cn", "zh-Hans-CN":"zh-Cn", "yue-Hans-CN" : "zh-Cn", "zh-HK": "zh-Tw", "zh-Hant":"zh-Tw", "zh-Hant-Cn":"zh-Tw", "zh-Hant-MO" : "zh-Tw", "zh-Hant-TW" : "zh-Tw", "zh-Hant-HK" : "zh-Tw", "yue-Hant-CN" : "zh-Tw"} def translate(filein, fileout): """ Translate the dom """ -
nesterchung revised this gist
Aug 10, 2018 . No changes.There are no files selected for viewing
-
nesterchung created this gist
Aug 10, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # File : xliff_translate.py # Author : Nester Chung <twntwn3838@gmail> # Date : 09.08.2018 # Last Modified Date: 10.08.2018 # Please install module before use this script: # lxml # googletrans import argparse import codecs import logging as log from lxml import etree from lxml.etree import tostring from googletrans import Translator NS = {'x':'urn:oasis:names:tc:xliff:document:1.2'} TRANSLATOR = Translator() #xliff language code mapping to google language code LM = {"zh-Hans":"zh-Cn"} def translate(filein, fileout): """ Translate the dom """ log.debug("read input file: %s", filein) locale_tree = etree.parse(filein) locale_root = locale_tree.getroot() for trans_node in locale_root.xpath('//x:trans-unit', namespaces=NS): for child in trans_node.xpath('./x:target', namespaces=NS): src_lang = trans_node.getparent().getparent().get('source-language') dest_lang = trans_node.getparent().getparent().get('target-language') source_string = trans_node.xpath('./x:source', namespaces=NS)[0].text tobj = TRANSLATOR.translate( source_string, src=LM.get(src_lang, src_lang), dest=LM.get(dest_lang, dest_lang)) log.debug("%s => %s", source_string, tobj.text) child.text = tobj.text tmp = tostring(locale_root, encoding='unicode') if fileout is None: print tmp else: with codecs.open(fileout, encoding='utf-8', mode='w+') as out: log.debug("write to %s", fileout) out.write(tmp) if __name__ == "__main__": PARSER = argparse.ArgumentParser(description='Translate xliff file') PARSER.add_argument('filein', metavar='INPUT', help='an XLIFF File need to be translate') PARSER.add_argument('-o', '--output', dest='fileout') PARSER.add_argument('-v', '--verbose', action='store_true') ARGS = PARSER.parse_args() if ARGS.verbose: log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG) else: log.basicConfig(format="%(levelname)s: %(message)s") translate(ARGS.filein, ARGS.fileout)