diff --git a/README.md b/README.md index cafa21c..75155fb 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,14 @@ # fastlane to XLIFF converter -Simple Python tool which can convert fastlane version of resources stored in files into XLIFF. +Simple Python tool which can convert fastlane version of resources stored in files into XLIFF and vice versa. This version of tool was tested with Python 2.7 and Trados. You can find more information about fastlane at: https://fastlane.tools/ ## Usage +### Conversion from fastlane to XLIFF + `python bin/fastlane2xliff.py ` Sample: @@ -15,6 +17,16 @@ Sample: `python bin/fastlane2xliff.py sample/src sample/result/sk-SK/tram.xlf "sk-SK"` +### Conversion from XLIFF to fastlane + + python bin/xliff2fastlane.py + +Sample: + + python bin/xliff2fastlane.py sample/result/cs-CZ/tram.xlf sample/src + + python bin/xliff2fastlane.py sample/result/sk-SK/tram.xlf sample/src + ## Further information You can find more information about development in Y Soft RnD at http://www.ysofters.com diff --git a/bin/xliff2fastlane.py b/bin/xliff2fastlane.py new file mode 100755 index 0000000..aa4b460 --- /dev/null +++ b/bin/xliff2fastlane.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Author: Juraj Michalek - Y Soft RnD - http://www.ysofters.com +# MIT License - see LICENSE.txt + +import io +import os +import sys +import xml.etree.ElementTree as ET + +def write_file_content(path, file_name, content): + """Read content of file.""" + if not os.path.exists(path): + os.makedirs(path) + f = io.open(path + file_name, "w", encoding = 'utf-8') + f.write(content) + f.close() + +def parse_xliff_for_os(target_dir_name, os_name, target_language, body_element): + """Convert XLIFF for OS into files for Fastlane.""" + for trans_unit in body_element: + trans_unit_id = trans_unit.attrib['id'] + trans_unit_target = '' + for trans_unit_element in trans_unit: + if trans_unit_element.tag.endswith('target'): + trans_unit_target = u'' + trans_unit_element.text + break + resource_path = target_dir_name + '/' + os_name + '/metadata/' + target_language + '/' + resource_file_name = trans_unit_id + '.txt' + write_file_content(resource_path, resource_file_name, trans_unit_target) + +if len(sys.argv) < 2: + print "Specify parameters:" + print " source xliff files" + print " target directory" + sys.exit(1) + +xliff_file_name = sys.argv[1] +target_dir_name = sys.argv[2] + +source_tree = ET.parse(xliff_file_name) +source_root = source_tree.getroot() +xliff_content = source_root.getchildren() + +for xliff_data in xliff_content: + parse_xliff_for_os(target_dir_name, xliff_data.attrib['original'], xliff_data.attrib['target-language'], xliff_data.getchildren()[0]) + +