diff --git a/babel/messages/checkers.py b/babel/messages/checkers.py index cf0fbcd39..6f58e4410 100644 --- a/babel/messages/checkers.py +++ b/babel/messages/checkers.py @@ -153,16 +153,20 @@ def _check_positional(results: list[tuple[str, str]]) -> bool: ) # otherwise the second string must not have names the first one - # doesn't have and the types of those included must be compatible + # doesn't have, and each type must match a type used by the same name + # in the first string. else: - type_map = dict(a) + type_map: dict[str, set[str]] = {} + for name, typechar in a: + type_map.setdefault(name, set()).add(typechar) for name, typechar in b: if name not in type_map: raise TranslationError(f'unknown named placeholder {name!r}') - elif not _compatible(typechar, type_map[name]): + elif not any(_compatible(typechar, source_type) for source_type in type_map[name]): + source_types = ', '.join(repr(source_type) for source_type in sorted(type_map[name])) raise TranslationError( f'incompatible format for placeholder {name!r}: ' - f'{typechar!r} and {type_map[name]!r} are not compatible', + f'{typechar!r} and {source_types} are not compatible', ) diff --git a/tests/messages/frontend/test_cli.py b/tests/messages/frontend/test_cli.py index a2b85bfba..80e5d5fef 100644 --- a/tests/messages/frontend/test_cli.py +++ b/tests/messages/frontend/test_cli.py @@ -18,6 +18,7 @@ import sys import time from datetime import datetime, timedelta +from gettext import GNUTranslations from io import StringIO import pytest @@ -419,6 +420,32 @@ def test_compile_catalog(cli): assert sys.stderr.getvalue() == f'catalog {po_file} is marked as fuzzy, skipping\n' +@pytest.mark.parametrize( + ('msgid', 'msgstr', 'value', 'expected'), + [ + ('Value: %(value)s (%(value)d)', 'Wert: %(value)d (%(value)s)', 42, 'Wert: 42 (42)'), + ('Code: %(value)d (0x%(value)x)', 'Code: %(value)d (0x%(value)x)', 42, 'Code: 42 (0x2a)'), + ('Name: %(value)s (%(value)r)', 'Name: %(value)r (%(value)s)', 'word', "Name: 'word' (word)"), + ], +) +def test_compile_catalog_repeated_named_types(cli, tmp_path, msgid, msgstr, value, expected): + catalog = Catalog(locale='de', fuzzy=False) + catalog.add(msgid, msgstr) + po_file = tmp_path / 'messages.po' + mo_file = tmp_path / 'messages.mo' + with po_file.open('wb') as fileobj: + write_po(fileobj, catalog) + + assert cli.run([ + 'pybabel', 'compile', '--locale', 'de', + '--input-file', str(po_file), '--output-file', str(mo_file), + ]) == 0 + + with mo_file.open('rb') as fileobj: + translations = GNUTranslations(fileobj) + assert translations.gettext(msgid) % {'value': value} == expected + + def test_compile_fuzzy_catalog(cli): po_file = get_po_file_path('de_DE') mo_file = po_file.replace('.po', '.mo') diff --git a/tests/messages/test_checkers.py b/tests/messages/test_checkers.py index 01194e467..9d6caba9e 100644 --- a/tests/messages/test_checkers.py +++ b/tests/messages/test_checkers.py @@ -370,6 +370,50 @@ def test_python_format_valid(msgid, msgstr): python_format(None, msg) +@pytest.mark.parametrize( + ('msgid', 'msgstr', 'value'), + [ + ('%(value)s %(value)d', '%(value)s %(value)d', 42), + ('%(value)d %(value)s', '%(value)s %(value)d', 42), + ('%(value)s %(value)d', '%(value)s', 42), + ('%(value)r %(value)s', '%(value)s %(value)r', 'word'), + ('%(value)x %(value)s', '%(value)s %(value)X', 42), + ('%(value)d %(value)x', '%(value)X %(value)i', 42), + ], +) +def test_python_format_repeated_named_types(msgid, msgstr, value): + arguments = {'value': value} + assert isinstance(msgid % arguments, str) + assert isinstance(msgstr % arguments, str) + python_format(None, Message(msgid, msgstr)) + + +@pytest.mark.parametrize( + ('msgid', 'msgstr', 'value'), + [ + ('%(value)s %(value)f', '%(value)x', 1.5), + ('%(value)s %(value)r', '%(value)d', 'word'), + ('%(value)s %(value)s', '%(value)d', 'word'), + ], +) +def test_python_format_repeated_named_types_invalid(msgid, msgstr, value): + arguments = {'value': value} + assert isinstance(msgid % arguments, str) + with pytest.raises(TypeError): + msgstr % arguments + with pytest.raises(TranslationError, match="incompatible format for placeholder 'value'"): + python_format(None, Message(msgid, msgstr)) + + +def test_python_format_repeated_named_types_plural_catalog(): + catalog = Catalog(locale='ru') + catalog.add( + ('%(value)s: %(value)d item', '%(value)s: %(value)d items'), + ('%(value)d (%(value)s)', '%(value)s (%(value)i)', '%(value)d'), + ) + assert list(catalog.check()) == [] + + @pytest.mark.parametrize( ('msgid', 'msgstr', 'arguments'), [