Created
May 12, 2020 08:02
-
-
Save kirmartuk/5bb87e82f82ec0ef862c4d96c88d56f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from googletrans import Translator | |
from pathlib import Path | |
original_code = open(".../res/values/strings.xml", "r") | |
# locals ex. 'en', 'ru', 'es' | |
locals = open(".../locals.txt", "r") | |
translator = Translator() | |
code = '' | |
for line in original_code.readlines(): | |
code += line | |
for locale in locals.readlines(): | |
path = Path(".../gen/values-" + locale.rstrip() + '/strings.xml') | |
path.parent.mkdir(parents=True, exist_ok=True) | |
generated_xml = '' | |
for i in code.split('\n'): | |
if i.find('<string') > -1: | |
name_start = i.find('"') + 1 | |
name_end = i.rfind('">') | |
value_start = i.find('>') + 1 | |
value_end = i.rfind('</') | |
name = i[name_start:name_end] | |
value = i[value_start:value_end] | |
generated_xml += ' <string name="{0}">{1}</string>\n'.format(name, | |
translator.translate( | |
value, | |
dest=locale.rstrip()) | |
.text) | |
else: | |
generated_xml += i | |
path.write_text(generated_xml) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment