Skip to content

Commit

Permalink
translator fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Manrique committed Nov 24, 2024
1 parent cdb4230 commit bbc3155
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,37 +148,35 @@ def translate_ios_strings():
def translate_android_or_common_strings():
for language_name in languages_to_translate:
language_to_translate = language_name.strip()
translated_file_directory = create_android_directories(language_to_translate)
print(" -> " + language_to_translate + " =========================")
tree = ElementTree.parse(INPUT_FILE)
root = tree.getroot()

for i in range(len(root)):
if 'translatable' not in root[i].attrib:
value = root[i].text
context = root[i].attrib.get('comment')
if value is not None:
params = {
'auth_key': API_KEY,
'text': value,
'source_lang': languages_from_translate,
"target_lang": language_to_translate
}
if context is not None:
params['context'] = context
if language_to_translate in languages_supporting_formality:
params["formality"] = "less"
request = requests.post("https://api.deepl.com/v2/translate", data=params)
result = request.json()

translated_file_directory = create_android_directories(language_to_translate)
print(" -> " + language_to_translate + " =========================")

tree = ElementTree.parse(INPUT_FILE)
root = tree.getroot()
for i in range(len(root)):
if 'translatable' not in root[i].attrib:
value = root[i].text
context = root[i].attrib.get('comment')
if value is not None:
params = {
'auth_key': API_KEY,
'text': value,
'source_lang': languages_from_translate,
"target_lang": language_to_translate
}
if context is not None:
params['context'] = context
if language_to_translate in languages_supporting_formality:
params["formality"] = "less"
request = requests.post("https://api.deepl.com/v2/translate", data=params)
result = request.json()

translated_text = result["translations"][0]["text"].strip()
root[i].text = translated_text.replace("'", "\\'")
print(value + "-->" + root[i].text)

translated_file = translated_file_directory + "/strings.xml"
translated_text = result["translations"][0]["text"].strip()
root[i].text = translated_text.replace("'", "\\'")
print(value + "-->" + root[i].text)

tree.write(translated_file, encoding='utf-8')
translated_file = translated_file_directory + "/strings.xml"
tree.write(translated_file, encoding='utf-8')


if PLATFORM != 'ios':
Expand Down

0 comments on commit bbc3155

Please sign in to comment.