Skip to content

Instantly share code, notes, and snippets.

@obiwankennedy
Created March 29, 2019 10:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obiwankennedy/7092ea8b271a54002c65060b463e4f43 to your computer and use it in GitHub Desktop.
Save obiwankennedy/7092ea8b271a54002c65060b463e4f43 to your computer and use it in GitHub Desktop.
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