Skip to content

Instantly share code, notes, and snippets.

@manjotsidhu
Created July 18, 2020 09:50
Show Gist options
  • Save manjotsidhu/236988d440234f51d8e0d58674001a7f to your computer and use it in GitHub Desktop.
Save manjotsidhu/236988d440234f51d8e0d58674001a7f to your computer and use it in GitHub Desktop.
PBRP Translation script
#
# Python Script for Translating
# pip install googletrans
#
import re
from googletrans import Translator
translate_lang = None
translator = Translator()
# input: string to translate
# dest : language to translate to [https://py-googletrans.readthedocs.io/en/latest/#googletrans-languages]
def translate_string(input, dest):
return translator.translate(input, dest=dest).text
def matcher(match):
global translate_lang
return match.group().replace(match.group(1), translate_string(match.group(1), translate_lang))
# input: input file name
# extension: file extension with [.]
# dest: output file name [only name, extension will be taken from input]
def translate(input, ext, dest):
global translate_lang
translate_lang = dest
in_file = open(input + '.' + ext, "r", encoding="utf-8")
contents = in_file.read()
in_file.close()
contents = re.sub('<string\sname="[^\n\r]+">([^\n\r]+)<\/string>', matcher, contents)
out = open(dest + '.' + ext, "w", encoding="utf-8")
out.write(contents)
out.close()
translate('en', 'xml', 'hi')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment