Skip to content

Commit e8158d1

Browse files
gh-130647: Add --omit-header option to pygettext (#130650)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 21a6a8a commit e8158d1

5 files changed

Lines changed: 33 additions & 4 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#: noheader.py:3
2+
msgid "Foo"
3+
msgstr ""
4+
5+
#: noheader.py:5
6+
msgid "Bar"
7+
msgstr ""
8+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from gettext import gettext as _
2+
3+
_('Foo')
4+
5+
_('Bar')

Lib/test/test_tools/test_i18n.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ def extract_from_snapshots():
606606
'custom_keywords.py': ('--keyword=foo', '--keyword=nfoo:1,2',
607607
'--keyword=pfoo:1c,2',
608608
'--keyword=npfoo:1c,2,3', '--keyword=_:1,2'),
609+
'noheader.py': ('--omit-header',),
609610
'multiple_keywords.py': ('--keyword=foo:1c,2,3', '--keyword=foo:1c,2',
610611
'--keyword=foo:1,2',
611612
# repeat a keyword to make sure it is extracted only once
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``--omit-header`` option to :program:`pygettext`.

Tools/i18n/pygettext.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@
124124
--width=columns
125125
Set width of output to columns.
126126
127+
--omit-header
128+
Do not write header to file.
129+
130+
This is useful for testing purposes because it eliminates a source of
131+
variance for generated .mo files.
132+
133+
Note: Using this option may lead to an error during compilation or other
134+
manipulation if the resulting file is not entirely in ASCII.
135+
127136
-x filename
128137
--exclude-file=filename
129138
Specify a file that contains a list of strings that are not be
@@ -629,9 +638,11 @@ def _is_string_const(self, node):
629638
def write_pot_file(messages, options, fp):
630639
timestamp = time.strftime('%Y-%m-%d %H:%M%z')
631640
encoding = fp.encoding if fp.encoding else 'UTF-8'
632-
print(pot_header % {'time': timestamp, 'version': __version__,
633-
'charset': encoding,
634-
'encoding': '8bit'}, file=fp)
641+
642+
if not options.omit_header:
643+
print(pot_header % {'time': timestamp, 'version': __version__,
644+
'charset': encoding,
645+
'encoding': '8bit'}, file=fp)
635646

636647
# Sort locations within each message by filename and lineno
637648
sorted_keys = [
@@ -691,7 +702,7 @@ def main():
691702
['extract-all', 'add-comments=?', 'default-domain=', 'escape',
692703
'help', 'keyword=', 'no-default-keywords',
693704
'add-location', 'no-location', 'output=', 'output-dir=',
694-
'style=', 'verbose', 'version', 'width=', 'exclude-file=',
705+
'style=', 'verbose', 'version', 'width=', 'omit-header', 'exclude-file=',
695706
'docstrings', 'no-docstrings',
696707
])
697708
except getopt.error as msg:
@@ -712,6 +723,7 @@ class Options:
712723
locationstyle = GNU
713724
verbose = 0
714725
width = 78
726+
omit_header = False
715727
excludefilename = ''
716728
docstrings = 0
717729
nodocstrings = {}
@@ -764,6 +776,8 @@ class Options:
764776
options.width = int(arg)
765777
except ValueError:
766778
usage(1, f'--width argument must be an integer: {arg}')
779+
elif opt in ('--omit-header',):
780+
options.omit_header = True
767781
elif opt in ('-x', '--exclude-file'):
768782
options.excludefilename = arg
769783
elif opt in ('-X', '--no-docstrings'):

0 commit comments

Comments
 (0)