2020-06-29 19:01:30 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright (C) 2020 Romain Failliot <romain.failliot@foolstep.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation; either version 2 of the license, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# this program. You may also obtain a copy of the GNU General Public License
|
|
|
|
# from the Free Software Foundation by visiting their web site
|
|
|
|
# (http://www.fsf.org/) or by writing to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
2020-06-29 19:58:30 +00:00
|
|
|
import argparse
|
2020-06-29 19:01:30 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
|
2021-11-24 15:41:11 +00:00
|
|
|
|
2020-06-29 19:58:30 +00:00
|
|
|
def check_translation(filename):
|
2021-11-24 15:41:11 +00:00
|
|
|
subprocess.run(['msgfmt', '-c', '-v', filename])
|
|
|
|
|
2020-06-29 19:58:30 +00:00
|
|
|
|
2020-06-29 19:01:30 +00:00
|
|
|
def update_translation(filename):
|
2021-11-24 15:41:11 +00:00
|
|
|
print(f'Updating translation file "{filename}"...')
|
2020-06-29 19:01:30 +00:00
|
|
|
|
|
|
|
# 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
|
2021-11-24 15:41:11 +00:00
|
|
|
emptypofile = os.path.join(tmpdir, f'{lang}.empty.po')
|
|
|
|
subprocess.run([
|
|
|
|
'msginit',
|
|
|
|
'--no-wrap',
|
|
|
|
'--no-translator',
|
|
|
|
'-l',
|
|
|
|
lang,
|
|
|
|
'-o',
|
|
|
|
emptypofile,
|
|
|
|
'-i',
|
|
|
|
'diffuse.pot'])
|
2020-06-29 19:01:30 +00:00
|
|
|
|
|
|
|
# Merge with the old translation
|
2021-11-24 15:41:11 +00:00
|
|
|
subprocess.run([
|
|
|
|
'msgmerge',
|
|
|
|
'-q',
|
|
|
|
'--no-wrap',
|
|
|
|
tmpfile,
|
|
|
|
emptypofile,
|
|
|
|
'-o',
|
|
|
|
filename])
|
2020-06-29 19:01:30 +00:00
|
|
|
|
|
|
|
# Validate translation
|
2021-11-24 15:41:11 +00:00
|
|
|
print(f'Validate "{filename}":')
|
2020-06-29 19:58:30 +00:00
|
|
|
check_translation(filename)
|
2020-06-29 19:01:30 +00:00
|
|
|
|
2021-11-24 15:41:11 +00:00
|
|
|
print('Update done.')
|
|
|
|
|
2020-06-29 19:01:30 +00:00
|
|
|
|
2020-06-29 19:58:30 +00:00
|
|
|
# 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')
|
|
|
|
|
|
|
|
# Parse command-line arguments
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Get PO files from command-line
|
|
|
|
po_files = args.po_files
|
|
|
|
po_files.sort()
|
|
|
|
|
|
|
|
if args.check_only:
|
|
|
|
for file in po_files:
|
2021-11-24 15:41:11 +00:00
|
|
|
print(f'Validate "{file}":')
|
2020-06-29 19:58:30 +00:00
|
|
|
check_translation(file)
|
|
|
|
exit(0)
|
|
|
|
|
2020-06-29 19:01:30 +00:00
|
|
|
# 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)
|