Skip to content

Commit 9644a86

Browse files
committed
Fix MergeCatalog options and update docs
1 parent ad322bd commit 9644a86

2 files changed

Lines changed: 55 additions & 61 deletions

File tree

babel/messages/frontend.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,30 +1025,30 @@ def run(self):
10251025

10261026

10271027
class MergeCatalog(CommandMixin):
1028-
description='updates translation PO file by merging them with updated template POT file with using compendium'
1029-
user_options=[
1030-
('input-files', None, 'def.po (obsolete translations) ref.pot (actual template)'),
1028+
description = 'update a PO file by merging it with a newer POT template, optionally using a compendium'
1029+
user_options = [
1030+
('input-files', None, 'exactly two input files: def.po (obsolete translations); ref.pot (current template)'),
10311031
('compendium=', 'C', 'additional library of message translations, may be specified more than once'),
1032-
('compendium-overwrite', '', 'overwrite mode of compendium'),
1033-
('no-compendium-comment', '', ''),
1034-
('update', 'U', 'pdate def.po, do nothing if def.po already up to date'),
1032+
('compendium-overwrite', None, 'overwrite existing translations with compendium entries'),
1033+
('no-compendium-comment', None, 'do not add a comment for translations taken from a compendium'),
1034+
('update', 'U', 'update def.po, do nothing if def.po already up to date'),
10351035
('output-file=', 'o', 'write output to specified file, the results are written '
10361036
'to standard output if no output file is specified'),
10371037
('backup', None, 'make a backup of def.po'),
1038-
('suffix=', None, 'override the usual backup suffix'),
1038+
('suffix=', None, 'use SUFFIX as backup suffix instead of ~ (tilde)'),
10391039
('no-fuzzy-matching', 'N', 'do not use fuzzy matching'),
1040-
('no-location', None, 'suppress \'#: filename:line\' lines'),
1041-
('width=', 'w', 'set output page width'),
1040+
('no-location', None, 'do not include location comments with filename and line number'),
1041+
('width=', 'w', 'set output line width (default 76)'),
10421042
('no-wrap', None, 'do not break long message lines, longer '
1043-
'than the output page width, into several lines'),
1043+
'than the output line width, into several lines'),
10441044
('sort-output', 's', 'generate sorted output'),
10451045
('sort-by-file', 'F', 'sort output by file location'),
10461046
]
10471047

10481048
as_args = 'input-files'
10491049

10501050
multiple_value_options = (
1051-
'compendium'
1051+
'compendium',
10521052
)
10531053

10541054
boolean_options = [
@@ -1081,9 +1081,11 @@ def initialize_options(self):
10811081

10821082
def finalize_options(self):
10831083
if not self.input_files or len(self.input_files) != 2:
1084-
raise OptionError('must be two po files')
1084+
raise OptionError(
1085+
f'exactly two input files are required (def.po and ref.pot), got: {self.input_files!r}'
1086+
)
10851087
if not self.output_file and not self.update:
1086-
raise OptionError('you must specify the output file or update existing')
1088+
raise OptionError('you must specify the output file or use --update')
10871089

10881090
if self.no_wrap and self.width:
10891091
raise OptionError("'--no-wrap' and '--width' are mutually exclusive")
@@ -1092,8 +1094,10 @@ def finalize_options(self):
10921094
elif self.width is not None:
10931095
self.width = int(self.width)
10941096

1095-
def _get_message_from_compendium(self, compendium):
1096-
for file_path in compendium:
1097+
def _get_messages_from_compendiums(self, compendium_paths):
1098+
if not compendium_paths:
1099+
return
1100+
for file_path in compendium_paths:
10971101
with open(file_path, 'r') as pofile:
10981102
catalog = read_po(pofile)
10991103
for message in catalog:
@@ -1111,19 +1115,17 @@ def run(self):
11111115
no_fuzzy_matching=self.no_fuzzy_matching
11121116
)
11131117

1114-
if self.compendium:
1115-
for message, compendium_path in self._get_message_from_compendium(self.compendium):
1116-
current = catalog[message.id]
1117-
if message.id in catalog and (not current.string or current.fuzzy or self.compendium_overwrite):
1118-
if self.compendium_overwrite and not current.fuzzy and current.string:
1119-
catalog.obsolete[message.id] = current.clone()
1118+
for message, compendium_path in self._get_messages_from_compendiums(self.compendium):
1119+
if (current := catalog.get(message.id)) and (not current.string or current.fuzzy or self.compendium_overwrite):
1120+
if self.compendium_overwrite and not current.fuzzy and current.string:
1121+
catalog.obsolete[message.id] = current.clone()
11201122

1121-
current.string = message.string
1122-
if current.fuzzy:
1123-
current.flags.remove('fuzzy')
1123+
current.string = message.string
1124+
if current.fuzzy:
1125+
current.flags.remove('fuzzy')
11241126

1125-
if not self.no_compendium_comment:
1126-
current.auto_comments.append(compendium_path)
1127+
if not self.no_compendium_comment:
1128+
current.auto_comments.append(compendium_path)
11271129

11281130
catalog.fuzzy = any(message.fuzzy for message in catalog)
11291131
output_path = def_file if self.update else self.output_file

docs/cmdline.rst

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,9 @@ and updates each of them.
251251
concat
252252
======
253253

254-
The `concat` command merges multiple PO files into a single one. If a message has
254+
The ``concat`` command merges multiple PO files into one. If a message has
255255
different translations in different PO files, the conflicting translations are
256-
marked with a conflict comment::
257-
#-#-#-#-# <file> (PROJECT VERSION) #-#-#-#-#
258-
and the message itself is marked with a `fuzzy` flag::
256+
marked with a conflict comment and the message itself is marked with a ``fuzzy`` flag::
259257

260258
$ pybabel concat --help
261259
Usage: pybabel concat [options] <input-files>
@@ -265,79 +263,73 @@ and the message itself is marked with a `fuzzy` flag::
265263
Options:
266264
-h, --help show this help message and exit
267265
-o OUTPUT_FILE, --output-file=OUTPUT_FILE
268-
write output to specified file
266+
write output to specified file, the results are written
267+
to standard output if no output file is specified or if it is '-'
269268
--less-than=NUMBER print messages with less than this many
270269
definitions, defaults to infinite if not set
271270
--more-than=NUMBER print messages with more than this many
272271
definitions, defaults to 0 if not set
273-
-u, unique shorthand for --less-than=2, requests
272+
-u, --unique shorthand for --less-than=2, requests
274273
that only unique messages be printed
275274
--use-first use first available translation for each
276275
message, don't merge several translations
277-
--no-location do not write '#: filename:line' lines
276+
--no-location do not include location comments with filename and line number
278277
-w WIDTH, --width=WIDTH
279-
set output page width
278+
set output line width (default 76)
280279
--no-wrap do not break long message lines, longer than
281-
the output page width, into several lines
280+
the output line width, into several lines
282281
-s, --sort-output generate sorted output
283282
-F, --sort-by-file sort output by file location
284283

285284
merge
286-
======
285+
=====
287286

288-
The `merge` command allows updating files using a compendium as a translation memory::
287+
The ``merge`` command allows updating files, optionally using a compendium as a translation memory::
289288

290-
$ pybabel concat --help
289+
$ pybabel merge --help
291290
Usage: pybabel merge [options] <input-files>
292291

293-
updates translation PO file by merging them with updated template
294-
POT file with using compendium
292+
update a PO file by merging it with a newer POT template, optionally using a compendium
295293

296294
Options:
297295
-C COMPENDIUM_FILE, --compendium=COMPENDIUM_FILE
298296
additional library of message translations, may
299297
be specified more than once
300298
--compendium-overwrite
301-
overwrite mode of compendium
299+
overwrite existing translations with compendium entries
302300
--no-compendium-comment
303-
do not add a comment indicating that the message is
304-
taken from the compendium
305-
-U, --update update def.po, do nothing if def.po already up to date,
301+
do not add a comment for translations taken from a compendium
302+
-U, --update update def.po, do nothing if def.po already up to date
306303
-o OUTPUT_FILE, --output-file=OUTPUT_FILE
307304
write output to specified file, the results are written
308305
to standard output if no output file is specified
309306
--backup make a backup of def.po
310-
--suffix=SUFFIX override the usual backup suffix (default '~')
307+
--suffix=SUFFIX use SUFFIX as backup suffix instead of ~ (tilde)
311308
-N, --no-fuzzy-matching
312309
do not use fuzzy matching
313-
--no-location suppress '#: filename:line' lines'
310+
--no-location do not include location comments with filename and line number
314311
-w WIDTH, --width=WIDTH
315-
set output page width
312+
set output line width (default 76)
316313
--no-wrap do not break long message lines, longer
317-
than the output page width, into several lines
314+
than the output line width, into several lines
318315
-s, --sort-output generate sorted output
319-
-F --sort-by-file sort output by file location
320-
321-
The compendium can be used in two modes:
322-
- Default mode: the translations from the compendium are used
323-
only if they are missing in the output file.
324-
325-
- Compendium overwrite mode: when using the ``compendium-overwrite`` option, translations
326-
from the compendium take priority and replace those in the output file. If a translation
327-
is used from the compendium, a comment noting the source is added
316+
-F, --sort-by-file sort output by file location
328317

329318
The ``input-files`` option accepts exactly two arguments: a file with obsolete translations, and
330319
the current template file for updating translations.
331320

332321
The ``compendium`` option can be specified multiple times to use several compendiums.
322+
The compendium can be used in two modes:
333323

334-
The ``backup`` option is used to create a backup copy of the def.po file, which contains
335-
obsolete translations.
324+
- Default mode: translations from the compendium are used only if they are missing in the output file.
325+
- Compendium overwrite mode: when using the ``--compendium-overwrite`` option, translations
326+
from the compendium take priority and replace those in the output file.
336327

328+
The ``backup`` option is used to create a backup copy of the def.po file before updating it.
337329
The ``suffix`` option allows you to specify a custom suffix for the backup file (defaulting to ``~``).
338330

339-
pybable concat and merge usage scenarios
340-
======
331+
concat and merge usage scenarios
332+
=================================
341333

342334
1. Merging Multiple PO Files (`concat`)
343335
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)