initial commit of fastlane2xliff with sample

This commit is contained in:
Juraj Michalek
2016-06-24 20:56:02 +02:00
commit 7630f96a4b
17 changed files with 229 additions and 0 deletions

21
LICENSE.txt Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2016 Juraj Michalek - Y Soft Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

21
README.md Normal file
View File

@@ -0,0 +1,21 @@
# fastlane to XLIFF converter
Simple Python tool which can convert fastlane version of resources stored in files into XLIFF.
This version of tool was tested with Python 2.7 and Trados.
## Usage
`python bin/fastlane2xliff.py <soure-directory> <target-xliff-file> <language-code>`
Sample:
`python bin/fastlane2xliff.py sample/src sample/result/cs-CZ/tram.xlf "cs-CZ"`
`python bin/fastlane2xliff.py sample/src sample/result/sk-SK/tram.xlf "sk-SK"`
## Further information
You can find more information about development in Y Soft RnD at http://www.ysofters.com
## Author
Juraj Michalek - https://github.com/georgik

121
bin/fastlane2xliff.py Executable file
View File

@@ -0,0 +1,121 @@
#!/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 glob
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import tostring
import xml.dom.minidom
def base_file_name(long_path):
"""Reduce file path to base file name without extension"""
return long_path.split('/')[-1].split('.')[0]
def get_file_content(path):
"""Read content of file."""
f = io.open(path, "r", encoding = 'utf-8')
data = ""
for line in f:
data += line
f.close()
return data
def get_target_resource_path(prefix_path, language):
"""
Return path with full language if it exist.
Return shorter path in case of e.g. ro if it exists.
Return path to english version.
"""
long_path = prefix_path + "/" + language
if os.path.exists(long_path):
return long_path
short_path = prefix_path + "/" + language.split('-')[0]
if os.path.exists(short_path):
return short_path
return prefix_path + "/en-US"
def parse_resources_for_os(os_name, xliff_element):
"""Parse resources for specific OS and append it to XLIFF element."""
meta_path = source_dir_name + "/" + os_name + "/metadata"
source_files = glob.glob(meta_path + "/en-US/*.txt")
locale_keys = map(base_file_name, source_files)
xliff_data = {}
target_resource_path = get_target_resource_path(meta_path, target_language)
for locale_key in locale_keys:
name = locale_key
value = get_file_content(meta_path + "/en-US/" + name + ".txt")
target = get_file_content(target_resource_path + "/" + name + ".txt")
# Do not include keys for empty resoruces
if len(value) == 0:
continue
xliff_data[name] = {
'source': value,
'target': target
}
file_element = ET.SubElement(xliff_element, 'file')
file_element.attrib['source-language'] = 'en-US'
file_element.attrib['target-language'] = target_language
file_element.attrib['datatype'] = 'plaintext'
file_element.attrib['original'] = os_name
bodyElement = ET.SubElement(file_element, 'body')
comment_counter = 0
for unit_id in xliff_data:
trans_unit_element = ET.SubElement(bodyElement, 'trans-unit')
trans_unit_element.attrib['id'] = unit_id
trans_unit_source = ET.SubElement(trans_unit_element, 'source')
trans_unit_source.text = xliff_data[unit_id]['source']
trans_unit_target = ET.SubElement(trans_unit_element, 'target')
if xliff_data[unit_id].has_key('target'):
trans_unit_target.text = xliff_data[unit_id]['target']
else:
print "Target missing, adding untranslated for:", unit_id
trans_unit_target.text = trans_unit_source.text
if trans_unit_source.text != trans_unit_target.text:
trans_unit_element.attrib['approved'] = 'yes'
trans_unit_target.attrib['state'] = 'translated'
else:
trans_unit_element.attrib['approved'] = 'no'
trans_unit_target.attrib['state'] = 'new'
if len(sys.argv) < 3:
print "Specify parameters:"
print " source directory with .txt files"
print " result xliff files"
print " target language code"
sys.exit(1)
source_dir_name = sys.argv[1]
xliff_file_name = sys.argv[2]
target_language = sys.argv[3]
namespace="urn:oasis:names:tc:xliff:document:1.1"
ET.register_namespace('', namespace)
xliff_element = ET.Element('xliff', xmlns=namespace)
xliff_element.attrib['version'] = '1.1'
parse_resources_for_os('iOS', xliff_element)
parse_resources_for_os('Android', xliff_element)
xml = xml.dom.minidom.parseString(tostring(xliff_element, encoding='UTF-8', method='html'))
output = open(xliff_file_name, 'wb')
output.write(xml.toprettyxml(indent=' ', encoding='UTF-8'))
output.close()

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1">
<file datatype="plaintext" original="iOS" source-language="en-US" target-language="cs-CZ">
<body>
<trans-unit approved="yes" id="name">
<source>Tram for iOS</source>
<target state="translated">Šalina pro iOS</target>
</trans-unit>
<trans-unit approved="yes" id="description">
<source>A tram (also known as tramcar; and in North America known as streetcar, trolley or trolley car) is a rail vehicle which runs on tracks along public urban streets, and also sometimes on a segregated right of way.</source>
<target state="translated">V brněnském hantecu se tramvaji říká šalina (zkomolením německého slovního spojení elektrische Linie [elektryše líne][1]) nebo šmirgl, naproti tomu v Ostravě se běžně užívá výraz tramvajka.</target>
</trans-unit>
</body>
</file>
<file datatype="plaintext" original="Android" source-language="en-US" target-language="cs-CZ">
<body>
<trans-unit approved="yes" id="name">
<source>Tram Android App</source>
<target state="translated">Šalina pro Android</target>
</trans-unit>
<trans-unit approved="yes" id="description">
<source>A tram (also known as tramcar; and in North America known as streetcar, trolley or trolley car) is a rail vehicle which runs on tracks along public urban streets, and also sometimes on a segregated right of way.</source>
<target state="translated">Šalina je v dnešní terminologii vozidlo nebo vlak tramvajové dráhy.</target>
</trans-unit>
</body>
</file>
</xliff>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1">
<file datatype="plaintext" original="iOS" source-language="en-US" target-language="sk-SK">
<body>
<trans-unit approved="yes" id="name">
<source>Tram for iOS</source>
<target state="translated">Električka pre iOS</target>
</trans-unit>
<trans-unit approved="yes" id="description">
<source>A tram (also known as tramcar; and in North America known as streetcar, trolley or trolley car) is a rail vehicle which runs on tracks along public urban streets, and also sometimes on a segregated right of way.</source>
<target state="translated">Električky disponujú oveľa väčšou prepravnou kapacitou ako autobusy či trolejbusy. Pohyb po koľajniciach umožňuje rýchlu a plynulú prepravu cestujúcich alebo nákladu v mestách a ich okolí.</target>
</trans-unit>
</body>
</file>
<file datatype="plaintext" original="Android" source-language="en-US" target-language="sk-SK">
<body>
<trans-unit approved="yes" id="name">
<source>Tram Android App</source>
<target state="translated">Električka pre Android</target>
</trans-unit>
<trans-unit approved="yes" id="description">
<source>A tram (also known as tramcar; and in North America known as streetcar, trolley or trolley car) is a rail vehicle which runs on tracks along public urban streets, and also sometimes on a segregated right of way.</source>
<target state="translated">Električka je koľajový dopravný prostriedok (jednoduchší a ľahší ako vlak) prevádzkovaný v mestskej a prímestskej hromadnej doprave.</target>
</trans-unit>
</body>
</file>
</xliff>

View File

@@ -0,0 +1 @@
Šalina je v dnešní terminologii vozidlo nebo vlak tramvajové dráhy.

View File

@@ -0,0 +1 @@
Šalina pro Android

View File

@@ -0,0 +1 @@
A tram (also known as tramcar; and in North America known as streetcar, trolley or trolley car) is a rail vehicle which runs on tracks along public urban streets, and also sometimes on a segregated right of way.

View File

@@ -0,0 +1 @@
Tram Android App

View File

@@ -0,0 +1 @@
Električka je koľajový dopravný prostriedok (jednoduchší a ľahší ako vlak) prevádzkovaný v mestskej a prímestskej hromadnej doprave.

View File

@@ -0,0 +1 @@
Električka pre Android

View File

@@ -0,0 +1 @@
V brněnském hantecu se tramvaji říká šalina (zkomolením německého slovního spojení elektrische Linie [elektryše líne][1]) nebo šmirgl, naproti tomu v Ostravě se běžně užívá výraz tramvajka.

View File

@@ -0,0 +1 @@
Šalina pro iOS

View File

@@ -0,0 +1 @@
A tram (also known as tramcar; and in North America known as streetcar, trolley or trolley car) is a rail vehicle which runs on tracks along public urban streets, and also sometimes on a segregated right of way.

View File

@@ -0,0 +1 @@
Tram for iOS

View File

@@ -0,0 +1 @@
Električky disponujú oveľa väčšou prepravnou kapacitou ako autobusy či trolejbusy. Pohyb po koľajniciach umožňuje rýchlu a plynulú prepravu cestujúcich alebo nákladu v mestách a ich okolí.

View File

@@ -0,0 +1 @@
Električka pre iOS