Skip to content

Instantly share code, notes, and snippets.

@iacchus
Last active June 22, 2022 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iacchus/fe530894a4790f813eddd7228c4a571c to your computer and use it in GitHub Desktop.
Save iacchus/fe530894a4790f813eddd7228c4a571c to your computer and use it in GitHub Desktop.
How to use Google Cloud Translation API to translate XLIFF file in Python (example)
#!/usr/bin/env python3
# USING
#
# This script is an example; for more information, inspect the classes
# translate.storage.xliff.xlifffilename and
# translate.storage.xliff.xliffunit from the lib Translate Toolkit:
#
# https://docs.translatehouse.org/_/downloads/translate-toolkit/en/stable-1.12.0/pdf/
#
# Also, the Google Cloud API documentation:
#
# https://cloud.google.com/translate/docs/reference/libraries/v2/python
#
# 1. Change the three globals below accordingly (in uppercase)
#
# 2. Install dependencies:
# pip install google-cloud-translate translate-toolkit\[all\]
#
# 3. Setup your Google Cloud Credentials in the
# envvar `GOOGLE_APPLICATION_CREDENTIALS`, for the project with
# the Translate API enabled, as in the docs:
#
# https://cloud.google.com/docs/authentication/getting-started
#
# 4. Make the file executable and run it. The output filename will
# contain the current date as the script is executed.
import datetime
from translate.storage.xliff import xlifffile
from google.cloud import translate_v2
now = datetime.datetime.now() # used to create the output file without
# overwriting anything
dumpfilename = now.strftime("%Y%m%d_%H%M%S-translation.txt")
XLIFF_FILENAME = "flarum-core-en.xlf"
SOURCE_LANGUAGE = "en"
TARGET_LANGUAGE = "pt"
a = open(XLIFF_FILENAME, 'r')
translate_client = translate_v2.Client() # have you creds ready here
b = a.read() # str()
c = b.encode('utf-8') # bytes()
d = xlifffile(inputfile=c, sourcelanguage=SOURCE_LANGUAGE,
targetlanguage=TARGET_LANGUAGE)
# d.getunits() -> list[xliffunit]
# e = d.getunits()[0] # first xliffunit, as `unit` in the `for` below
totalunits = len(d.getunits())
for number, unit in enumerate(d.getunits()):
res = translate_client.translate(unit.gettarget(),
target_language=TARGET_LANGUAGE,
format_="text")
howmuch = "({}/{})".format(number, totalunits)
source = res['input']
translation = res['translatedText']
print(howmuch, source, translation, sep='\n', end="\n\n")
unit.settarget(translation, lang=TARGET_LANGUAGE)
d.savefile(dumpfilename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment