@@ -108,6 +108,7 @@ def __init__(self, path: Path, language: str = "default") -> None:
108108 self .language : str = language
109109 self .strings : Dict [str , str ] = {} # key -> string value
110110 self .plurals : Dict [str , Dict [str , str ]] = {} # key -> {quantity -> text}
111+ self .modified : bool = False # Flag to track if any changes are made
111112 self .parse_file ()
112113
113114 def parse_file (self ) -> None :
@@ -235,6 +236,11 @@ def update_xml_file(resource: AndroidResourceFile) -> None:
235236 This version uses lxml and makes sure that if new elements are appended, the first new element is
236237 correctly indented.
237238 """
239+
240+ # Only update if the resource was modified
241+ if not resource .modified :
242+ return
243+
238244 try :
239245 parser = etree .XMLParser (remove_blank_text = False )
240246 tree = etree .parse (str (resource .path ), parser )
@@ -331,6 +337,8 @@ def update_xml_file(resource: AndroidResourceFile) -> None:
331337 f .truncate ()
332338
333339 logger .info (f"Updated XML file: { resource .path } " )
340+ # Reset the modified flag after update.
341+ resource .modified = False
334342 except Exception as e :
335343 logger .error (f"Error writing XML file { resource .path } : { e } " )
336344
@@ -511,6 +519,7 @@ def auto_translate_resources(
511519 translated = validate_translation (source_text , translated , target_language = lang )
512520 logger .info (f"Validated string '{ key } ': now '{ translated } '" )
513521 res .strings [key ] = translated
522+ res .modified = True # Mark as modified
514523
515524 # Record the translation for reporting.
516525 translation_log [module .name ][lang ]["strings" ].append ({
@@ -536,6 +545,7 @@ def auto_translate_resources(
536545 merged = generated_plural .copy ()
537546 merged .update (current_map )
538547 res .plurals [plural_name ] = merged
548+ res .modified = True # Mark as modified
539549 logger .info (f"Translated plural group '{ plural_name } ' for language '{ lang } ': { res .plurals [plural_name ]} " )
540550 if validate_translations :
541551 for plural_key in generated_plural :
@@ -552,7 +562,9 @@ def auto_translate_resources(
552562 })
553563 except Exception as e :
554564 logger .error (f"Error translating plural '{ plural_name } ': { e } " )
555- update_xml_file (res )
565+ # Only update the XML file if modifications were made.
566+ if res .modified :
567+ update_xml_file (res )
556568 return translation_log
557569
558570# ------------------------------------------------------------------------------
0 commit comments