Add --remove-obsolete option to update-translation.py

This commit is contained in:
Romain Failliot 2021-11-24 11:12:45 -05:00
parent 7b2992ab3f
commit 034d72bf8b
2 changed files with 47 additions and 49 deletions

View File

@ -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

View File

@ -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)
# Create a new .po file for this language
emptypofile = os.path.join(tmpdir, f'{lang}.empty.po')
with tempfile.NamedTemporaryFile() as ftmp:
# Create temporary .po file for this language
subprocess.run([
'msginit',
'--no-wrap',
'--no-translator',
'-l',
lang,
'-o',
emptypofile,
ftmp.name,
'-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='+',
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('-c', '--check-only', action='store_true',
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:
if args.check_only:
# Validate PO files
for file in po_files:
print(f'Validate "{file}":')
check_translation(file)
exit(0)
# Create temporary working directory
tmpdir = tempfile.mkdtemp()
try:
if args.remove_obsolete:
# Remove obsolete messages
for file in po_files:
remove_obsolete_messages(file)
exit(0)
# Update PO files
for file in po_files:
update_translation(file)
finally:
# Remove working directory
shutil.rmtree(tmpdir)