Add --remove-obsolete option to update-translation.py
This commit is contained in:
parent
7b2992ab3f
commit
034d72bf8b
|
@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Added a `--remove-obsolete` option for the update-translation.py script
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- Updated the translation files
|
- Updated the translation files
|
||||||
- Renamed POTFILES.in to POTFILES
|
- Renamed POTFILES.in to POTFILES
|
||||||
|
|
|
@ -22,80 +22,75 @@
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import shutil
|
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
|
|
||||||
def check_translation(filename):
|
def check_translation(filename):
|
||||||
|
print(f'Validate file "{filename}":')
|
||||||
subprocess.run(['msgfmt', '-c', '-v', 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):
|
def update_translation(filename):
|
||||||
print(f'Updating translation file "{filename}"...')
|
print(f'Updating translation file "{filename}"...')
|
||||||
|
|
||||||
# Get language
|
# Get language
|
||||||
lang = os.path.splitext(filename)[0]
|
lang = os.path.splitext(filename)[0]
|
||||||
|
|
||||||
# Move existing .po file to working directory
|
with tempfile.NamedTemporaryFile() as ftmp:
|
||||||
tmpfile = os.path.join(tmpdir, filename)
|
# Create temporary .po file for this language
|
||||||
shutil.move(filename, tmpfile)
|
|
||||||
|
|
||||||
# Create a new .po file for this language
|
|
||||||
emptypofile = os.path.join(tmpdir, f'{lang}.empty.po')
|
|
||||||
subprocess.run([
|
subprocess.run([
|
||||||
'msginit',
|
'msginit',
|
||||||
'--no-wrap',
|
|
||||||
'--no-translator',
|
'--no-translator',
|
||||||
'-l',
|
'-l',
|
||||||
lang,
|
lang,
|
||||||
'-o',
|
'-o',
|
||||||
emptypofile,
|
ftmp.name,
|
||||||
'-i',
|
'-i',
|
||||||
'diffuse.pot'])
|
'diffuse.pot'])
|
||||||
|
|
||||||
# Merge with the old translation
|
# Merge with the previous translation
|
||||||
subprocess.run([
|
subprocess.run(['msgmerge', '-q', '--no-wrap', '-o', filename, filename, ftmp.name])
|
||||||
'msgmerge',
|
|
||||||
'-q',
|
|
||||||
'--no-wrap',
|
|
||||||
tmpfile,
|
|
||||||
emptypofile,
|
|
||||||
'-o',
|
|
||||||
filename])
|
|
||||||
|
|
||||||
# Validate translation
|
# Validate translation
|
||||||
print(f'Validate "{filename}":')
|
|
||||||
check_translation(filename)
|
check_translation(filename)
|
||||||
|
|
||||||
print('Update done.')
|
print('Update done.')
|
||||||
|
|
||||||
|
|
||||||
# Setup argument parser
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser(description='Update translation files (.po).')
|
# Setup argument parser
|
||||||
parser.add_argument('po_files', metavar='filename.po', nargs='+',
|
parser = argparse.ArgumentParser(description='Update translation files (.po).')
|
||||||
|
parser.add_argument('po_files', metavar='filename.po', nargs='+',
|
||||||
help='the translation file')
|
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')
|
help='check the PO files')
|
||||||
|
|
||||||
# Parse command-line arguments
|
# Parse command-line arguments
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Get PO files from command-line
|
# Get PO files from command-line
|
||||||
po_files = args.po_files
|
po_files = args.po_files
|
||||||
po_files.sort()
|
po_files.sort()
|
||||||
|
|
||||||
if args.check_only:
|
if args.check_only:
|
||||||
|
# Validate PO files
|
||||||
for file in po_files:
|
for file in po_files:
|
||||||
print(f'Validate "{file}":')
|
|
||||||
check_translation(file)
|
check_translation(file)
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
# Create temporary working directory
|
if args.remove_obsolete:
|
||||||
tmpdir = tempfile.mkdtemp()
|
# Remove obsolete messages
|
||||||
try:
|
for file in po_files:
|
||||||
|
remove_obsolete_messages(file)
|
||||||
|
exit(0)
|
||||||
|
|
||||||
# Update PO files
|
# Update PO files
|
||||||
for file in po_files:
|
for file in po_files:
|
||||||
update_translation(file)
|
update_translation(file)
|
||||||
finally:
|
|
||||||
# Remove working directory
|
|
||||||
shutil.rmtree(tmpdir)
|
|
||||||
|
|
Loading…
Reference in New Issue