#!/usr/bin/env python # -*- coding: utf-8 -*- # File : gist:2f2acad6fce7ebc8baff1288633309d7/xliff_translate.py # Author : Nester Chung # 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", "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 """ 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='input filename') PARSER.add_argument('-o', '--output', dest='fileout' help='output filename') 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)