From 034d72bf8b409953cdf8f4dc2c3230f20795f31b Mon Sep 17 00:00:00 2001 From: Romain Failliot Date: Wed, 24 Nov 2021 11:12:45 -0500 Subject: [PATCH] Add --remove-obsolete option to update-translation.py --- CHANGELOG.md | 3 ++ po/update-translations.py | 93 ++++++++++++++++++--------------------- 2 files changed, 47 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6e78ed..5270eb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Added +- Added a `--remove-obsolete` option for the update-translation.py script + ### Changed - Updated the translation files - Renamed POTFILES.in to POTFILES diff --git a/po/update-translations.py b/po/update-translations.py index 306c20a..8f759c7 100755 --- a/po/update-translations.py +++ b/po/update-translations.py @@ -22,80 +22,75 @@ import argparse import os import subprocess -import shutil import tempfile def check_translation(filename): + print(f'Validate file "{filename}":') subprocess.run(['msgfmt', '-c', '-v', filename]) +def remove_obsolete_messages(filename): + print(f'Removing obsolete messages in file "{filename}"...') + subprocess.run(['msgattrib', '--no-obsolete', '-o', filename, filename]) + + def update_translation(filename): print(f'Updating translation file "{filename}"...') # Get language lang = os.path.splitext(filename)[0] - # Move existing .po file to working directory - tmpfile = os.path.join(tmpdir, filename) - shutil.move(filename, tmpfile) + with tempfile.NamedTemporaryFile() as ftmp: + # Create temporary .po file for this language + subprocess.run([ + 'msginit', + '--no-translator', + '-l', + lang, + '-o', + ftmp.name, + '-i', + 'diffuse.pot']) - # Create a new .po file for this language - emptypofile = os.path.join(tmpdir, f'{lang}.empty.po') - subprocess.run([ - 'msginit', - '--no-wrap', - '--no-translator', - '-l', - lang, - '-o', - emptypofile, - '-i', - 'diffuse.pot']) - - # Merge with the old translation - subprocess.run([ - 'msgmerge', - '-q', - '--no-wrap', - tmpfile, - emptypofile, - '-o', - filename]) + # Merge with the previous translation + subprocess.run(['msgmerge', '-q', '--no-wrap', '-o', filename, filename, ftmp.name]) # Validate translation - print(f'Validate "{filename}":') check_translation(filename) print('Update done.') -# Setup argument parser -parser = argparse.ArgumentParser(description='Update translation files (.po).') -parser.add_argument('po_files', metavar='filename.po', nargs='+', - help='the translation file') -parser.add_argument('-c', '--check-only', action='store_true', - help='check the PO files') +if __name__ == '__main__': + # Setup argument parser + parser = argparse.ArgumentParser(description='Update translation files (.po).') + parser.add_argument('po_files', metavar='filename.po', nargs='+', + help='the translation file') + parser.add_argument('--remove-obsolete', action='store_true', + help='remove obsolete (#~) messages') + parser.add_argument('--check-only', action='store_true', + help='check the PO files') -# Parse command-line arguments -args = parser.parse_args() + # Parse command-line arguments + args = parser.parse_args() -# Get PO files from command-line -po_files = args.po_files -po_files.sort() + # Get PO files from command-line + po_files = args.po_files + po_files.sort() -if args.check_only: - for file in po_files: - print(f'Validate "{file}":') - check_translation(file) - exit(0) + if args.check_only: + # Validate PO files + for file in po_files: + check_translation(file) + exit(0) + + if args.remove_obsolete: + # Remove obsolete messages + for file in po_files: + remove_obsolete_messages(file) + exit(0) -# Create temporary working directory -tmpdir = tempfile.mkdtemp() -try: # Update PO files for file in po_files: update_translation(file) -finally: - # Remove working directory - shutil.rmtree(tmpdir)