Skip to content

Commit 0a6388d

Browse files
committed
Fix PR issues
* Rename file_name to filename * Adding fuzzy flag to message parameterized in 'add_conflict' * Replace usage scenarious to cmdline.rst * Rename to ConcatenateCatalog
1 parent dd44348 commit 0a6388d

5 files changed

Lines changed: 69 additions & 69 deletions

File tree

babel/messages/catalog.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import re
1414
import os
1515
from collections.abc import Iterable, Iterator
16+
from collections import defaultdict
1617
from copy import copy
1718
from difflib import SequenceMatcher
1819
from email import message_from_string
@@ -341,7 +342,7 @@ def _force_text(s: str | bytes, encoding: str = 'utf-8', errors: str = 'strict')
341342

342343
class ConflictInfo(TypedDict):
343344
message: Message
344-
file_name: str
345+
filename: str
345346
project: str
346347
version: str
347348

@@ -389,7 +390,7 @@ def __init__(
389390
self.locale = locale
390391
self._header_comment = header_comment
391392
self._messages: dict[str | tuple[str, str], Message] = {}
392-
self._conflicts: dict[str | tuple[str, str], list[ConflictInfo]] = {}
393+
self._conflicts: dict[str | tuple[str, str], list[ConflictInfo]] = defaultdict(list)
393394

394395
self.project = project or 'PROJECT'
395396
self.version = version or 'VERSION'
@@ -756,18 +757,17 @@ def __setitem__(self, id: _MessageID, message: Message) -> None:
756757
f"Expected sequence but got {type(message.string)}"
757758
self._messages[key] = message
758759

759-
def add_conflict(self, message: Message, file_name: str, project: str, version: str):
760+
def add_conflict(self, message: Message, filename: str, project: str, version: str, fuzzy: bool = True):
760761
key = message.id
761-
if key not in self._conflicts:
762-
self._conflicts[key] = []
763-
764762
self._conflicts[key].append({
765763
'message': message,
766-
'file_name': file_name,
764+
'filename': filename,
767765
'project': project,
768766
'version': version,
769767
})
770-
message.flags |= {'fuzzy'}
768+
769+
if fuzzy:
770+
message.flags |= {'fuzzy'}
771771

772772
def add(
773773
self,

babel/messages/frontend.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -958,8 +958,8 @@ def run(self):
958958
continue
959959

960960
if count > 1 and not self.use_first and diff_string_count > 1:
961-
file_name = os.path.basename(path)
962-
catalog.add_conflict(message, file_name, template.project, template.version)
961+
filename = os.path.basename(path)
962+
catalog.add_conflict(message, filename, template.project, template.version)
963963

964964
catalog[message.id] = message
965965

@@ -1109,7 +1109,7 @@ class CommandLineInterface:
11091109
'init': 'create new message catalogs from a POT file',
11101110
'update': 'update existing message catalogs from a POT file',
11111111
'concat': 'concatenates and merges the specified PO files',
1112-
'merge': 'combines two Uniforum-style PO files into one',
1112+
'merge': 'combines two PO files into one',
11131113
}
11141114

11151115
command_classes = {

babel/messages/pofile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ def _format_conflict(key: str | tuple[str, str], conflicts: list[ConflictInfo],
653653
for conflict in conflicts:
654654
message = conflict['message']
655655
if message.context:
656-
yield from _format_conflict_comment(conflict['file_name'], conflict['project'], conflict['version'], prefix=prefix)
656+
yield from _format_conflict_comment(conflict['filename'], conflict['project'], conflict['version'], prefix=prefix)
657657
yield f"{prefix}msgctxt {normalize(message.context, prefix=prefix, width=width)}\n"
658658

659659
if isinstance(key, (list, tuple)):
@@ -665,7 +665,7 @@ def _format_conflict(key: str | tuple[str, str], conflicts: list[ConflictInfo],
665665

666666
for conflict in conflicts:
667667
message = conflict['message']
668-
yield from _format_conflict_comment(conflict['file_name'], conflict['project'], conflict['version'], prefix=prefix)
668+
yield from _format_conflict_comment(conflict['filename'], conflict['project'], conflict['version'], prefix=prefix)
669669
if isinstance(key, (list, tuple)):
670670
for idx in range(catalog.num_plurals):
671671
try:

docs/cmdline.rst

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,65 @@ The compendium can be used in two modes:
326326
from the compendium take priority and replace those in the output file. If a translation
327327
is used from the compendium, a comment noting the source is added
328328

329-
The ``input-files`` option includes def.po, a file with obsolete translations, and ref.pot,
329+
The ``input-files`` option accepts exactly two arguments: a file with obsolete translations, and
330330
the current template file for updating translations.
331331

332332
The ``compendium`` option can be specified multiple times to use several compendiums.
333333

334334
The ``backup`` option is used to create a backup copy of the def.po file, which contains
335-
obsolete translations
335+
obsolete translations.
336336

337-
The ``suffix`` option allows you to specify a custom suffix for the backup file
338-
By default, a standard suffix ``~`` is appended to the backup file's name,
337+
The ``suffix`` option allows you to specify a custom suffix for the backup file (defaulting to ``~``).
338+
339+
pybable concat and merge usage scenarios
340+
======
341+
342+
1. Merging Multiple PO Files (`concat`)
343+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
344+
345+
**Usage:**
346+
`pybabel concat [options] <po_files>`
347+
Suppose you manage a project with several PO files for the same language (for example, modules or plugins have their own translations), and you want to combine them into a single file for further work or for delivery to translators.
348+
349+
**Example:**
350+
351+
.. code-block:: shell
352+
353+
pybabel concat -o merged.po module1.po module2.po module3.po
354+
355+
**Features:**
356+
357+
- If the same string has different translations in different files, the resulting file for that string will include a special comment ``#-#-#-#-# <file> (PROJECT VERSION) #-#-#-#-#`` and the message will be marked with the ``fuzzy`` flag—this is useful for later manual conflict resolution.
358+
- You can keep only unique strings using the ``-u`` (`--less-than=2`) option.
359+
- Use `--use-first` to take only the first encountered translation for each string, skipping automatic merging of multiple options.
360+
- Output can be sorted alphabetically or by source file (options `-s`, `-F`).
361+
362+
**Typical Use Case:**
363+
364+
A project has translations from different teams. Before releasing, you need to gather all translations into one file, resolve possible conflicts, and provide the finalized version to translators for review.
365+
366+
367+
2. Updating Translations with a Template and Compendium (`merge`)
368+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
369+
370+
**Usage:**
371+
`pybabel merge [options] def.po ref.pot`
372+
You need to update an existing translation file (`def.po`) based on a new template (`ref.pot`), reusing translations from an additional translation memory (compendium).
373+
374+
**Example:**
375+
376+
.. code-block:: shell
377+
378+
pybabel merge -C my-compendium.po --backup def.po ref.pot
379+
380+
**Features:**
381+
382+
- The compendium (`-C`) allows you to pull translations from a shared translation memory. Multiple compendiums can be used.
383+
- By default, translations from the compendium are used only for new or missing entries in `def.po`.
384+
- The `--compendium-overwrite` option allows overwriting existing translations with those found in the compendium (helpful for terminology standardization).
385+
- When a translation from the compendium is used, a comment is automatically added (this can be disabled with `--no-compendium-comment`).
386+
- The `--backup` flag saves a backup copy of your file before updating (`~` suffix by default, configurable with `--suffix`).
387+
388+
**Typical Use Case:**
389+
390+
After a release, a new translation template is provided. The team decides to enrich the translation by leveraging a common compendium in order to improve quality and unify terms. The merge command is run with the compendium and backup options enabled.

docs/concat_merge_usage.rst

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)