@@ -289,6 +289,45 @@ def compile_translations(
289289 raise RuntimeError (f"Compilation failed: { e .stderr } " ) from e
290290
291291
292+ def cleanup_doc_translations (
293+ directory : typing .Union [str , os .PathLike ],
294+ locale_dir : str = "doc/locale" ,
295+ ) -> None :
296+ """Clean up Sphinx documentation translation files.
297+
298+ This function removes volatile header lines (POT-Creation-Date, Last-Translator)
299+ from all .po files in the specified locale directory to avoid unnecessary merge
300+ conflicts when cherry-picking commits between branches.
301+
302+ Args:
303+ directory: The root directory of the project.
304+ locale_dir: The relative path to the locale directory (default: "doc/locale").
305+
306+ Raises:
307+ FileNotFoundError: The locale directory does not exist.
308+ """
309+ locale_path = os .path .join (directory , locale_dir )
310+ if not os .path .exists (locale_path ):
311+ print (f"Error: Locale directory { locale_path } does not exist." )
312+ raise FileNotFoundError (f"Locale directory { locale_path } does not exist." )
313+
314+ # Find all .po files in the locale directory
315+ po_files = []
316+ for root , _ , files in os .walk (locale_path ):
317+ for file in files :
318+ if file .endswith (".po" ):
319+ po_files .append (os .path .join (root , file ))
320+
321+ if not po_files :
322+ print (f"No .po files found in { locale_path } " )
323+ return
324+
325+ print (f"Cleaning up { len (po_files )} .po files in { locale_path } ..." )
326+ for po_file in po_files :
327+ _cleanup_po_file (po_file )
328+ print ("Cleanup completed successfully." )
329+
330+
292331def _get_def (option : str ) -> str | None :
293332 """Get the default value from environment variables or return None."""
294333 return os .environ .get (f"I18N_{ option .upper ()} " )
@@ -336,6 +375,19 @@ def main():
336375 help = "Language codes to translate (space-separated, e.g., 'fr it')" ,
337376 )
338377
378+ cleanup_doc_parser = subparsers .add_parser (
379+ "cleanup-doc" , help = "Clean up Sphinx documentation translation files"
380+ )
381+ cleanup_doc_parser .add_argument (
382+ "--directory" , required = True , help = "Project root directory"
383+ )
384+ cleanup_doc_parser .add_argument (
385+ "--locale-dir" ,
386+ required = False ,
387+ default = "doc/locale" ,
388+ help = "Relative path to the locale directory" ,
389+ )
390+
339391 args = parser .parse_args ()
340392
341393 if args .command == "compile" :
@@ -347,6 +399,8 @@ def main():
347399 args .copyright_holder ,
348400 args .languages ,
349401 )
402+ elif args .command == "cleanup-doc" :
403+ cleanup_doc_translations (args .directory , args .locale_dir )
350404 else :
351405 parser .print_help ()
352406 raise ValueError (f"Unknown command: { args .command } . Use 'scan' or 'compile'." )
0 commit comments