Skip to content

Commit 3097912

Browse files
committed
babel.messages.catalog: deduplicate _to_fuzzy_match_key logic
1 parent 56071c9 commit 3097912

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

babel/messages/catalog.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -827,15 +827,13 @@ def update(
827827
self._messages = OrderedDict()
828828

829829
# Prepare for fuzzy matching
830-
fuzzy_candidates = []
830+
fuzzy_candidates = {}
831831
if not no_fuzzy_matching:
832-
fuzzy_candidates = {}
833832
for msgid in messages:
834833
if msgid and messages[msgid].string:
835834
key = self._key_for(msgid)
836835
ctxt = messages[msgid].context
837-
modified_key = key.lower().strip()
838-
fuzzy_candidates[modified_key] = (key, ctxt)
836+
fuzzy_candidates[self._to_fuzzy_match_key(key)] = (key, ctxt)
839837
fuzzy_matches = set()
840838

841839
def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, str] | str) -> None:
@@ -883,12 +881,11 @@ def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, s
883881
else:
884882
if not no_fuzzy_matching:
885883
# do some fuzzy matching with difflib
886-
if isinstance(key, tuple):
887-
matchkey = key[0] # just the msgid, no context
888-
else:
889-
matchkey = key
890-
matches = get_close_matches(matchkey.lower().strip(),
891-
fuzzy_candidates.keys(), 1)
884+
matches = get_close_matches(
885+
self._to_fuzzy_match_key(key),
886+
fuzzy_candidates.keys(),
887+
1,
888+
)
892889
if matches:
893890
modified_key = matches[0]
894891
newkey, newctxt = fuzzy_candidates[modified_key]
@@ -912,6 +909,14 @@ def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, s
912909
# used to update the catalog
913910
self.creation_date = template.creation_date
914911

912+
def _to_fuzzy_match_key(self, key: tuple[str, str] | str) -> str:
913+
"""Converts a message key to a string suitable for fuzzy matching."""
914+
if isinstance(key, tuple):
915+
matchkey = key[0] # just the msgid, no context
916+
else:
917+
matchkey = key
918+
return matchkey.lower().strip()
919+
915920
def _key_for(self, id: _MessageID, context: str | None = None) -> tuple[str, str] | str:
916921
"""The key for a message is just the singular ID even for pluralizable
917922
messages, but is a ``(msgid, msgctxt)`` tuple for context-specific

0 commit comments

Comments
 (0)