Skip to content

Commit

Permalink
Remove obsolete translations
Browse files Browse the repository at this point in the history
If original string has been removed then remove the obsolete translation
as well
  • Loading branch information
ashishb committed Apr 6, 2023
1 parent 9849562 commit 0e49fe1
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _normalize_response(text: str) -> str:


def _translate(
strings: typing.Dict[str, ET.Element],
src_strings: typing.Dict[str, ET.Element],
target_lang: str,
translated_string_xml_file: str,
):
Expand All @@ -94,22 +94,33 @@ def _translate(
if target_lang == "pt-rBR":
target_lang = "pt"
logging.info("Translating into '%s'...", translated_string_xml_file)
translated_str = _get_strings_to_translate(translated_string_xml_file)
translated_strings = _get_strings_to_translate(translated_string_xml_file)
translations_to_add: typing.Dict[str, ET.Element] = dict()
num_translated = 0
translate = googletrans.Translator()
translator = googletrans.Translator()

for k in strings:
if translated_str.get(k, None) is not None:
# Strings that have been translated but are no longer part of the main
# file
translations_to_remove = list(
filter(lambda x: x not in src_strings, translated_strings)
)
logging.info(
'%d strings will be removed from %s',
len(translations_to_remove),
translated_string_xml_file,
)

for k in src_strings:
if translated_strings.get(k, None) is not None:
continue
logging.debug("Requires translation in '%s' -> '%s'", target_lang, k)
num_translated += 1
try:
translation = translate.translate(strings[k].text, dest=target_lang)
translation = translator.translate(src_strings[k].text, dest=target_lang)
except:
logging.error("Failed to translate")
continue
element = copy.deepcopy(strings[k])
element = copy.deepcopy(src_strings[k])
element.text = _normalize_response(translation.text)
translations_to_add[k] = element

Expand All @@ -119,11 +130,15 @@ def _translate(
target_lang,
translated_string_xml_file,
)
if num_translated == 0:
if num_translated == 0 and len(translations_to_remove) == 0:
return

xml_tree = ET.parse(translated_string_xml_file)
qualified_strings_root = xml_tree.getroot()
for k in translations_to_remove:
for qualified_string in qualified_strings_root:
if qualified_string.attrib.get(_XML_ATTR_NAME) == k:
qualified_strings_root.remove(qualified_string)
for k in translations_to_add:
qualified_strings_root.append(translations_to_add[k])
logging.info("Writing changes to '%s'", translated_string_xml_file)
Expand Down

0 comments on commit 0e49fe1

Please sign in to comment.