Skip to content

Commit 49e6003

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 13e3330 commit 49e6003

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
@@ -14,6 +14,7 @@
1414
import re
1515
import os
1616
from collections.abc import Iterable, Iterator
17+
from collections import defaultdict
1718
from copy import copy
1819
from difflib import SequenceMatcher
1920
from email import message_from_string
@@ -360,7 +361,7 @@ def _force_text(s: str | bytes, encoding: str = 'utf-8', errors: str = 'strict')
360361

361362
class ConflictInfo(TypedDict):
362363
message: Message
363-
file_name: str
364+
filename: str
364365
project: str
365366
version: str
366367

@@ -408,7 +409,7 @@ def __init__(
408409
self.locale = locale
409410
self._header_comment = header_comment
410411
self._messages: dict[str | tuple[str, str], Message] = {}
411-
self._conflicts: dict[str | tuple[str, str], list[ConflictInfo]] = {}
412+
self._conflicts: dict[str | tuple[str, str], list[ConflictInfo]] = defaultdict(list)
412413

413414
self.project = project or 'PROJECT'
414415
self.version = version or 'VERSION'
@@ -789,18 +790,17 @@ def __setitem__(self, id: _MessageID, message: Message) -> None:
789790
)
790791
self._messages[key] = message
791792

792-
def add_conflict(self, message: Message, file_name: str, project: str, version: str):
793+
def add_conflict(self, message: Message, filename: str, project: str, version: str, fuzzy: bool = True):
793794
key = message.id
794-
if key not in self._conflicts:
795-
self._conflicts[key] = []
796-
797795
self._conflicts[key].append({
798796
'message': message,
799-
'file_name': file_name,
797+
'filename': filename,
800798
'project': project,
801799
'version': version,
802800
})
803-
message.flags |= {'fuzzy'}
801+
802+
if fuzzy:
803+
message.flags |= {'fuzzy'}
804804

805805
def add(
806806
self,

babel/messages/frontend.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -993,8 +993,8 @@ def run(self):
993993
continue
994994

995995
if count > 1 and not self.use_first and diff_string_count > 1:
996-
file_name = os.path.basename(path)
997-
catalog.add_conflict(message, file_name, template.project, template.version)
996+
filename = os.path.basename(path)
997+
catalog.add_conflict(message, filename, template.project, template.version)
998998

999999
catalog[message.id] = message
10001000

@@ -1144,7 +1144,7 @@ class CommandLineInterface:
11441144
'init': 'create new message catalogs from a POT file',
11451145
'update': 'update existing message catalogs from a POT file',
11461146
'concat': 'concatenates and merges the specified PO files',
1147-
'merge': 'combines two Uniforum-style PO files into one',
1147+
'merge': 'combines two PO files into one',
11481148
}
11491149

11501150
command_classes = {

babel/messages/pofile.py

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

663663
if isinstance(key, (list, tuple)):
@@ -669,7 +669,7 @@ def _format_conflict(key: str | tuple[str, str], conflicts: list[ConflictInfo],
669669

670670
for conflict in conflicts:
671671
message = conflict['message']
672-
yield from _format_conflict_comment(conflict['file_name'], conflict['project'], conflict['version'], prefix=prefix)
672+
yield from _format_conflict_comment(conflict['filename'], conflict['project'], conflict['version'], prefix=prefix)
673673
if isinstance(key, (list, tuple)):
674674
for idx in range(catalog.num_plurals):
675675
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)