From 321bf595121f9327368af8bf5e850d9d145360c2 Mon Sep 17 00:00:00 2001 From: "Tomas R." Date: Mon, 3 Mar 2025 17:57:01 +0100 Subject: gh-130453: pygettext: Allow overriding default keywords when using `--keyword` (GH-130709) --- Lib/test/test_tools/i18n_data/custom_keywords.pot | 16 +++++++++++----- Lib/test/test_tools/i18n_data/custom_keywords.py | 4 ++++ Lib/test/test_tools/test_i18n.py | 2 +- .../2025-02-28-23-24-03.gh-issue-130453.EK0Vk_.rst | 1 + Tools/i18n/pygettext.py | 5 ++++- 5 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Tools-Demos/2025-02-28-23-24-03.gh-issue-130453.EK0Vk_.rst diff --git a/Lib/test/test_tools/i18n_data/custom_keywords.pot b/Lib/test/test_tools/i18n_data/custom_keywords.pot index 48df2e7..03a9cba 100644 --- a/Lib/test/test_tools/i18n_data/custom_keywords.pot +++ b/Lib/test/test_tools/i18n_data/custom_keywords.pot @@ -15,31 +15,37 @@ msgstr "" "Generated-By: pygettext.py 1.5\n" -#: custom_keywords.py:9 custom_keywords.py:10 +#: custom_keywords.py:10 custom_keywords.py:11 msgid "bar" msgstr "" -#: custom_keywords.py:12 +#: custom_keywords.py:13 msgid "cat" msgid_plural "cats" msgstr[0] "" msgstr[1] "" -#: custom_keywords.py:13 +#: custom_keywords.py:14 msgid "dog" msgid_plural "dogs" msgstr[0] "" msgstr[1] "" -#: custom_keywords.py:15 +#: custom_keywords.py:16 msgctxt "context" msgid "bar" msgstr "" -#: custom_keywords.py:17 +#: custom_keywords.py:18 msgctxt "context" msgid "cat" msgid_plural "cats" msgstr[0] "" msgstr[1] "" +#: custom_keywords.py:34 +msgid "overridden" +msgid_plural "default" +msgstr[0] "" +msgstr[1] "" + diff --git a/Lib/test/test_tools/i18n_data/custom_keywords.py b/Lib/test/test_tools/i18n_data/custom_keywords.py index 01ea56c..ba0ffe7 100644 --- a/Lib/test/test_tools/i18n_data/custom_keywords.py +++ b/Lib/test/test_tools/i18n_data/custom_keywords.py @@ -4,6 +4,7 @@ from gettext import ( pgettext as pfoo, npgettext as npfoo, gettext as bar, + gettext as _, ) foo('bar') @@ -28,3 +29,6 @@ pfoo('context') # 'npfoo' requires at least 3 arguments npfoo('context') npfoo('context', 'cat') + +# --keyword should override the default keyword +_('overridden', 'default') diff --git a/Lib/test/test_tools/test_i18n.py b/Lib/test/test_tools/test_i18n.py index d73fcff..2ba0861 100644 --- a/Lib/test/test_tools/test_i18n.py +++ b/Lib/test/test_tools/test_i18n.py @@ -525,7 +525,7 @@ def extract_from_snapshots(): 'comments.py': ('--add-comments=i18n:',), 'custom_keywords.py': ('--keyword=foo', '--keyword=nfoo:1,2', '--keyword=pfoo:1c,2', - '--keyword=npfoo:1c,2,3'), + '--keyword=npfoo:1c,2,3', '--keyword=_:1,2'), } for filename, args in snapshots.items(): diff --git a/Misc/NEWS.d/next/Tools-Demos/2025-02-28-23-24-03.gh-issue-130453.EK0Vk_.rst b/Misc/NEWS.d/next/Tools-Demos/2025-02-28-23-24-03.gh-issue-130453.EK0Vk_.rst new file mode 100644 index 0000000..684fd9a --- /dev/null +++ b/Misc/NEWS.d/next/Tools-Demos/2025-02-28-23-24-03.gh-issue-130453.EK0Vk_.rst @@ -0,0 +1 @@ +Make it possible to override default keywords in :program:`pygettext`. diff --git a/Tools/i18n/pygettext.py b/Tools/i18n/pygettext.py index 0f5f32c7..0ec2570 100755 --- a/Tools/i18n/pygettext.py +++ b/Tools/i18n/pygettext.py @@ -729,12 +729,15 @@ def main(): # calculate all keywords try: - options.keywords = dict(parse_spec(spec) for spec in options.keywords) + custom_keywords = dict(parse_spec(spec) for spec in options.keywords) except ValueError as e: print(e, file=sys.stderr) sys.exit(1) + options.keywords = {} if not no_default_keywords: options.keywords |= DEFAULTKEYWORDS + # custom keywords override default keywords + options.keywords |= custom_keywords # initialize list of strings to exclude if options.excludefilename: -- cgit v0.12