Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/test/test_tools/test_msgfmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ def test_no_input_file(self):
def test_nonexistent_file(self):
assert_python_failure(msgfmt, 'nonexistent.po')

def test_statistics(self):
with temp_cwd():
res = assert_python_ok(msgfmt, '--statistics', data_dir / "general.po")
out = res.out.decode('utf-8').strip()
self.assertEqual('7 translated messages, 1 untranslated message.', out)


def update_catalog_snapshots():
for po_file in data_dir.glob('*.po'):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``--statistics`` option to :program:`msgfmt`.
24 changes: 23 additions & 1 deletion Tools/i18n/msgfmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
-V
--version
Display version information and exit.

--statistics
Print statistics about translations.
"""

import os
Expand Down Expand Up @@ -229,11 +232,12 @@ def make(filename, outfile):
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'hVo:',
['help', 'version', 'output-file='])
['help', 'version', 'output-file=', 'statistics'])
except getopt.error as msg:
usage(1, msg)

outfile = None
print_statistics = False
# parse options
for opt, arg in opts:
if opt in ('-h', '--help'):
Expand All @@ -243,6 +247,8 @@ def main():
sys.exit(0)
elif opt in ('-o', '--output-file'):
outfile = arg
elif opt in ('--statistics',):
print_statistics = True
# do it
if not args:
print('No input file given', file=sys.stderr)
Expand All @@ -252,6 +258,22 @@ def main():
for filename in args:
make(filename, outfile)

if print_statistics:
strings = translated = 0
for msgid, msgstr in MESSAGES.items():
if msgid == b'':
continue
strings += 1
if msgstr.strip():
translated += 1

untranslated = strings - translated
message = f"{translated} translated message{'s' if translated != 1 else ''}"
if untranslated > 0:
message += f", {untranslated} untranslated message{'s' if untranslated != 1 else ''}"
message += "."
print(message)


if __name__ == '__main__':
main()
Loading