Merge pull request #50 from MightyCreak/fix-translations

Improve update-translations.py and its documentation
This commit is contained in:
Creak 2020-06-29 16:15:28 -04:00 committed by GitHub
commit 01888e39a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 25 deletions

View File

@ -27,33 +27,51 @@ Creating a new translation
Updating an existing translation Updating an existing translation
-------------------------------- --------------------------------
1. Move the existing .po file: Use `update-translations.py` to update one or more PO files.
$ mv ja.po old.po Here is an example with `ja.po` and `ko.po`:
2. Create an empty .po file for the translation: Command-line:
```sh
./update-translations.py ja.po ko.po
```
$ xgettext -s -o diffuse.pot -L Python ../src/usr/bin/diffuse Output:
$ msginit -l ja -o empty.po -i diffuse.pot ```
Generate 'diffuse.pot'.
Updating translation file 'ja.po'...
Created /tmp/tmp0gtniydu/ja.empty.po.
Validate ja.po:
183 translated messages, 2 untranslated messages.
Update done.
Updating translation file 'ko.po'...
Created /tmp/tmp0gtniydu/ko.empty.po.
Validate ko.po:
183 translated messages, 2 untranslated messages.
Update done.
```
3. Merge the old translations: Then use the text editor of your choice to complete the translations.
$ msgmerge old.po empty.po -o ja.po
4. Clean up:
$ rm old.po empty.po
5. Manually complete in the translations in the .po file:
$ vi ja.po
Validate a translation Validate a translation
---------------------- ----------------------
1. Attempt to compile the .po file and note any warnings: Use `update-translations.py` to validate one or more PO files.
$ msgfmt -c -v ja.po Here is an example with `ja.po` and `ko.po`:
Command-line:
```sh
./update-translations.py --check-only ja.po ko.po
```
Output:
```
Validate ja.po:
183 translated messages, 2 untranslated messages.
Validate ko.po:
183 translated messages, 2 untranslated messages.
```
System Integration System Integration
================== ==================

View File

@ -19,6 +19,7 @@
# (http://www.fsf.org/) or by writing to the Free Software Foundation, Inc., # (http://www.fsf.org/) or by writing to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import argparse
import glob import glob
import os import os
import subprocess import subprocess
@ -28,6 +29,9 @@ import tempfile
def generate_pot_file(): def generate_pot_file():
subprocess.run(["xgettext", "-s", "-o", "diffuse.pot", "-L", "Python", "../src/usr/bin/diffuse"]) subprocess.run(["xgettext", "-s", "-o", "diffuse.pot", "-L", "Python", "../src/usr/bin/diffuse"])
def check_translation(filename):
subprocess.run(["msgfmt", "-c", "-v", filename])
def update_translation(filename): def update_translation(filename):
print(f"Updating translation file '{filename}'...") print(f"Updating translation file '{filename}'...")
@ -46,21 +50,42 @@ def update_translation(filename):
subprocess.run(["msgmerge", "-q", "--no-wrap", tmpfile, emptypofile, "-o", filename]) subprocess.run(["msgmerge", "-q", "--no-wrap", tmpfile, emptypofile, "-o", filename])
# Validate translation # Validate translation
print(f"Validation:") print(f"Validate {filename}:")
subprocess.run(["msgfmt", "-c", "-v", filename]) check_translation(filename)
print(f"Update done.") print(f"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')
parser.add_argument('--no-pot-generation', action='store_true',
help='don\'t generate the POT file')
# 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:
print(f"Validate {file}:")
check_translation(file)
exit(0)
if not args.no_pot_generation:
# Generate diffuse.pot file # Generate diffuse.pot file
print("Generate 'diffuse.pot'.")
generate_pot_file() generate_pot_file()
# Create temporary working directory # Create temporary working directory
tmpdir = tempfile.mkdtemp() tmpdir = tempfile.mkdtemp()
try: try:
# Update PO files # Update PO files
po_files = glob.glob("*.po", recursive=False)
po_files.sort()
for file in po_files: for file in po_files:
update_translation(file) update_translation(file)
finally: finally: