I made a Python script to scaffold language files from the original in english to your language using Google Cloud Translation API. Google Translation uses AI and is currently good and getting better, and it reduces a lot the translation work needed to be done manually.
This script is more like an example. It needs:
- A Google Cloud project with the Translate API enabled.
- The credentials of a service user with access to that credentials configured locally.
- The file with the text to be translated in XLIFF format (
.xlf
) which can be downloaded via @rob006 weblate website.
- The globals in the code below, for
TARGET_LANGUAGE
and XLIFF_FILENAME
should be edited accordingly.
- Then just execute the script normally. It will output a translated file with the current date, which then can be edited by some software like
lokalize
- It uses
google-cloud-translate
and translate-toolkit
: install withpip install google-cloud-translate translate-toolkit\[all\]
.
- More info on the comments.
file: google-cloud-translate-xliff.py
#!/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 to not overwrite existing files.
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)