Created
March 29, 2019 10:22
-
-
Save obiwankennedy/7092ea8b271a54002c65060b463e4f43 to your computer and use it in GitHub Desktop.
Automatic translation of ts file (Qt i18n file) with Yandex
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 characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
import urllib.request | |
import xml.etree.ElementTree as ET | |
from yandex.Translater import Translater | |
import time | |
lang_dest="fr" | |
# Change paths | |
source_1="/home/user/to/app1_fr.ts" | |
source_2="/home/user/to/app2_fr.ts" | |
source_3="/home/user/to/app3_fr.ts" | |
yandexKey="Put your API key here" | |
tr = Translater() | |
tr.set_key(yandexKey) | |
tr.set_from_lang('en') # from english | |
tr.set_to_lang('fr') # to french | |
translations = [source_1,source_2,source_3] | |
print("# Start translation") | |
for trans in translations: | |
tree = ET.parse(trans) | |
root = tree.getroot() | |
for message in root.findall('context/message'): | |
source = message.find('source').text | |
transtext = message.find('translation').text | |
if transtext is None or len(transtext)>0: | |
tr.set_text(source) | |
value = tr.translate() | |
message.find('translation').text = value | |
time.sleep(1) | |
tree.write(trans) | |
print("# end translation - please edit ts file to remove unfinished status and to add proper xml header.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment