Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions babel/messages/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,15 +827,13 @@ def update(
self._messages = OrderedDict()

# Prepare for fuzzy matching
fuzzy_candidates = []
fuzzy_candidates = {}
if not no_fuzzy_matching:
fuzzy_candidates = {}
for msgid in messages:
if msgid and messages[msgid].string:
key = self._key_for(msgid)
ctxt = messages[msgid].context
modified_key = key.lower().strip()
fuzzy_candidates[modified_key] = (key, ctxt)
fuzzy_candidates[self._to_fuzzy_match_key(key)] = (key, ctxt)
fuzzy_matches = set()

def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, str] | str) -> None:
Expand Down Expand Up @@ -883,12 +881,11 @@ def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, s
else:
if not no_fuzzy_matching:
# do some fuzzy matching with difflib
if isinstance(key, tuple):
matchkey = key[0] # just the msgid, no context
else:
matchkey = key
matches = get_close_matches(matchkey.lower().strip(),
fuzzy_candidates.keys(), 1)
matches = get_close_matches(
self._to_fuzzy_match_key(key),
fuzzy_candidates.keys(),
1,
)
if matches:
modified_key = matches[0]
newkey, newctxt = fuzzy_candidates[modified_key]
Expand All @@ -912,6 +909,14 @@ def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, s
# used to update the catalog
self.creation_date = template.creation_date

def _to_fuzzy_match_key(self, key: tuple[str, str] | str) -> str:
"""Converts a message key to a string suitable for fuzzy matching."""
if isinstance(key, tuple):
matchkey = key[0] # just the msgid, no context
else:
matchkey = key
return matchkey.lower().strip()

def _key_for(self, id: _MessageID, context: str | None = None) -> tuple[str, str] | str:
"""The key for a message is just the singular ID even for pluralizable
messages, but is a ``(msgid, msgctxt)`` tuple for context-specific
Expand Down