Skip to content

Instantly share code, notes, and snippets.

@eumel8
Forked from xen/1-usage.txt
Created September 3, 2017 16:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eumel8/957a3c40ec5f483edf3b4f030fc50210 to your computer and use it in GitHub Desktop.
Save eumel8/957a3c40ec5f483edf3b4f030fc50210 to your computer and use it in GitHub Desktop.
i18njson translate using Google Translate
$ python i18njson-translate.py --help
Usage: i18njson-translate.py [OPTIONS] INPUTFILE OUTPUT
Process and translate .po or JSON files and generate translated .po file
in result. Can work with exist .po files and if in initial file msgid
dissapear then mark translaed string as obsolete. Try to play nice with
version control systems and follow initial file order format, you will get
predicteable diffs.
Options:
--lang TEXT From what language text should be translated. Use one of
Google Translate languages codes. Example: 'en'
--to TEXT Target language should be in result file
--paid Use paid API with key
--ftype [json|po] Accept input file in json or po format
--help Show this message and exit.
import os
import click
import polib
from collections import OrderedDict
import commentjson as json
try:
import urllib2 as request
from urllib import quote
except:
from urllib import request
from urllib.parse import quote
AGENT = u"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:36.0) Gecko/20100101 Firefox/36.0"
URL = u"http://translate.google.com/translate_a/t?client=p&ie=UTF-8&oe=UTF-8" + \
u"&sl={lang}&tl={to}&text={text}"
API_KEY = u"" # Insert API key here
def call_paid_api(message, lang, to):
from googleapiclient.discovery import build
service = build('translate', 'v2', developerKey=API_KEY)
res = service.translations().list(source=lang, target=to, q=message).execute()
return res['translations'][0]['translatedText']
def call_api(message, lang, to):
quoted = quote(message, '')
headers = {'User-Agent': AGENT}
req = request.Request(
url=URL.format(lang=lang, to=to, text=quoted),
headers=headers)
r = request.urlopen(req)
result = r.read().decode('utf-8')
return json.loads(result)['sentences'][0]['trans']
@click.command()
@click.argument('inputfile', type=click.Path())
@click.argument('output', type=click.Path())
@click.option('--lang', help="From what language text should be translated." + \
" Use one of Google Translate languages codes. Example: 'en'")
@click.option('--to', help='Target language should be in result file')
@click.option('--paid', help='Use paid API with key',
default=False, is_flag=True)
@click.option('--ftype', help='Accept input file in json or po format',
type=click.Choice(['json', 'po']))
def process(inputfile, output, lang, to, paid, ftype):
""" Process and translate .po or JSON files and generate translated .po
file in result. Can work with exist .po files and if in initial file
msgid dissapear then mark translaed string as obsolete. Try to play nice
with version control systems and follow initial file order format,
you will get predicteable diffs.
"""
if ftype == 'json':
items = json.load(open(inputfile), object_pairs_hook=OrderedDict)
else:
items = OrderedDict()
ifile = polib.pofile(inputfile)
for entry in ifile:
items[entry.msgid] = entry.msgstr
created = False
if os.path.exists(output):
po = polib.pofile(output)
else:
po = polib.POFile()
created = True
po.metadata = {
'Project-Id-Version': '1.0',
'Report-Msgid-Bugs-To': 'you@example.com',
'POT-Creation-Date': '2007-10-18 14:00+0100',
'PO-Revision-Date': '2007-10-18 14:00+0100',
'Last-Translator': 'you <you@example.com>',
'Language-Team': 'English <yourteam@example.com>',
'MIME-Version': '1.0',
'Content-Type': 'text/plain; charset=utf-8',
'Content-Transfer-Encoding': '8bit',
}
for k, v in items.items():
if paid:
translated = call_paid_api(v, lang, to)
else:
translated = call_api(v, lang, to)
entry = po.find(k, by="msgid", include_obsolete_entries=True)
if entry:
entry.msgstr = translated
entry.obsolete = False # mark not obsolete anymore
else:
entry = polib.POEntry(msgid=k, msgstr=translated)
po.append(entry)
for entry in po:
if not entry.msgid in items:
click.echo("msgid '{}' marked as obsolete".format(entry.msgid))
entry.obsolete = True
if created:
po.save(fpath=output)
else:
po.save()
if __name__ == '__main__':
process()
click
polib
commentjson
google-api-python-client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment