Skip to content

Commit 63bb71a

Browse files
committed
In fuzzy matching, also .lower().strip() fuzzy candidates
This seems intended at easing fuzzy matching with trivial edits in the msgstr (changing case and adding whitespace), but it was only done on the new msgstr, not on the old mgstr candidates, so it was possible for merging catalogs to miss messages.
1 parent 08af5e2 commit 63bb71a

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

babel/messages/catalog.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -803,10 +803,13 @@ def update(
803803
# Prepare for fuzzy matching
804804
fuzzy_candidates = []
805805
if not no_fuzzy_matching:
806-
fuzzy_candidates = {
807-
self._key_for(msgid): messages[msgid].context
808-
for msgid in messages if msgid and messages[msgid].string
809-
}
806+
fuzzy_candidates = {}
807+
for msgid in messages:
808+
if msgid and messages[msgid].string:
809+
key = self._key_for(msgid)
810+
ctxt = messages[msgid].context
811+
modified_key = key.lower().strip()
812+
fuzzy_candidates[modified_key] = (key, ctxt)
810813
fuzzy_matches = set()
811814

812815
def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, str] | str) -> None:
@@ -861,8 +864,8 @@ def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, s
861864
matches = get_close_matches(matchkey.lower().strip(),
862865
fuzzy_candidates.keys(), 1)
863866
if matches:
864-
newkey = matches[0]
865-
newctxt = fuzzy_candidates[newkey]
867+
modified_key = matches[0]
868+
newkey, newctxt = fuzzy_candidates[modified_key]
866869
if newctxt is not None:
867870
newkey = newkey, newctxt
868871
_merge(message, newkey, key)

tests/messages/test_catalog.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,16 @@ def test_update_message_updates_comments(self):
121121

122122
def test_update_fuzzy_matching_with_case_change(self):
123123
cat = catalog.Catalog()
124-
cat.add('foo', 'Voh')
124+
cat.add('FOO', 'Voh')
125125
cat.add('bar', 'Bahr')
126126
tmpl = catalog.Catalog()
127-
tmpl.add('Foo')
127+
tmpl.add('foo')
128128
cat.update(tmpl)
129129
assert len(cat.obsolete) == 1
130-
assert 'foo' not in cat
130+
assert 'FOO' not in cat
131131

132-
assert cat['Foo'].string == 'Voh'
133-
assert cat['Foo'].fuzzy is True
132+
assert cat['foo'].string == 'Voh'
133+
assert cat['foo'].fuzzy is True
134134

135135
def test_update_fuzzy_matching_with_char_change(self):
136136
cat = catalog.Catalog()

0 commit comments

Comments
 (0)