Skip to content

Commit 7af3cd4

Browse files
committed
Add cleanup function for Sphinx documentation translation files to prevent merge conflicts
1 parent a920b44 commit 7af3cd4

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

doc/release_notes/release_3.13.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
* Integrated cleanup step into `generate_translation_files()` after `.po` file creation
1212
* Ensures cleaner translation file management and reduces noise in commit history
1313

14+
✨ New features:
15+
16+
* New `cleanup-doc` command for Sphinx documentation translation files
17+
* Added `cleanup_doc_translations()` function to clean up `.po` files in `doc/locale/` directories
18+
* Removes `POT-Creation-Date` and `Last-Translator` headers from all Sphinx-generated translation files
19+
* Usage: `python -m guidata.utils.translations cleanup-doc --directory .`
20+
* Helps avoid merge conflicts when cherry-picking commits between branches (e.g., `release``develop`)
21+
* Optional `--locale-dir` argument to specify custom locale directory path (defaults to `doc/locale`)
22+
1423
## Version 3.13.4 (2025-12-03) ##
1524

1625
🛠️ Bug fixes:

guidata/utils/translations.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
292331
def _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

Comments
 (0)