Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-77393: Add --statistics opt to msgfmt.py #132136

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 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,17 @@ 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('8 translated messages, 1 untranslated message.', out)
# Multiple input files
res = assert_python_ok(msgfmt, '--statistics', '-o', 'temp.mo', data_dir / "general.po", data_dir / "fuzzy.po")
out = res.out.decode('utf-8').strip()
self.assertIn('general.po: 8 translated messages, 1 untranslated message.', out)
self.assertIn('fuzzy.po: 0 translated messages.', out)
Comment on lines +177 to +181
Copy link
Contributor Author

@StanFromIreland StanFromIreland Apr 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also a test for #53950 :-) Do we want a separate test too?



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`.
33 changes: 31 additions & 2 deletions 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 All @@ -38,6 +41,7 @@


MESSAGES = {}
empty_translations = 0 # Counter for empty translations for --statistics


def usage(code, msg=''):
Expand All @@ -49,12 +53,14 @@ def usage(code, msg=''):

def add(ctxt, id, str, fuzzy):
"Add a non-fuzzy translation to the dictionary."
global MESSAGES
global MESSAGES, empty_translations
if not fuzzy and str:
if ctxt is None:
MESSAGES[id] = str
else:
MESSAGES[b"%b\x04%b" % (ctxt, id)] = str
elif not fuzzy and not str:
empty_translations += 1


def generate():
Expand Down Expand Up @@ -99,6 +105,11 @@ def generate():


def make(filename, outfile):
# see gh-issue: 53950
global MESSAGES, empty_translations
MESSAGES.clear()
empty_translations = 0

ID = 1
STR = 2
CTXT = 3
Expand Down Expand Up @@ -229,11 +240,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 +255,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 +266,21 @@ def main():
for filename in args:
make(filename, outfile)

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

message = (f"{os.path.basename(filename) + ': ' if len(args) > 1 else ''}"
f"{translated} translated message{'s' if translated != 1 else ''}")
if empty_translations > 0:
message += f", {empty_translations} untranslated message{'s' if empty_translations != 1 else ''}"
message += "."
print(message)


if __name__ == '__main__':
main()