Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatic translation of ts file (Qt i18n file) with Yandex
#!/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
You can’t perform that action at this time.