From 307b04cef88225bd49b8ffd34c076755cd042e05 Mon Sep 17 00:00:00 2001 From: kimsanha Date: Thu, 17 Sep 2026 16:12:51 +0900 Subject: [PATCH] Ignore errors in fuzzy messages excluded from compilation --- babel/messages/frontend.py | 6 ++++- tests/messages/frontend/test_compile.py | 33 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py index 89826e834..d57cf59eb 100644 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -271,7 +271,11 @@ def _run_domain(self, domain): self.log.info('catalog %s is marked as fuzzy, skipping', po_file) continue - catalogs_and_errors[catalog] = catalog_errors = list(catalog.check()) + catalogs_and_errors[catalog] = catalog_errors = [ + (message, errors) + for message, errors in catalog.check() + if self.use_fuzzy or not message.fuzzy + ] for message, errors in catalog_errors: for error in errors: self.log.error('error: %s:%d: %s', po_file, message.lineno, error) diff --git a/tests/messages/frontend/test_compile.py b/tests/messages/frontend/test_compile.py index 3db413dac..4e2efd6ad 100644 --- a/tests/messages/frontend/test_compile.py +++ b/tests/messages/frontend/test_compile.py @@ -12,10 +12,14 @@ from __future__ import annotations +from gettext import GNUTranslations + import pytest from babel.messages import frontend +from babel.messages.catalog import Catalog from babel.messages.frontend import OptionError +from babel.messages.pofile import write_po from tests.messages.consts import TEST_PROJECT_DISTRIBUTION_DATA, data_dir from tests.messages.utils import Distribution @@ -41,3 +45,32 @@ def test_no_directory_or_input_file_specified(compile_catalog_cmd): compile_catalog_cmd.output_file = 'dummy' with pytest.raises(OptionError): compile_catalog_cmd.finalize_options() + + +@pytest.mark.parametrize('fuzzy', [False, True]) +@pytest.mark.parametrize('use_fuzzy', [False, True]) +def test_compile_checks_only_included_fuzzy_messages(tmp_path, caplog, fuzzy, use_fuzzy): + catalog = Catalog(locale='en', fuzzy=False) + catalog.add('Hello %(name)s', 'Hello %(other)s', flags=['fuzzy'] if fuzzy else []) + catalog.add('Goodbye', 'Bye') + po_file = tmp_path / 'messages.po' + mo_file = tmp_path / 'messages.mo' + with po_file.open('wb') as outfile: + write_po(outfile, catalog) + + cmd = frontend.CompileCatalog() + cmd.input_file = str(po_file) + cmd.output_file = str(mo_file) + cmd.use_fuzzy = use_fuzzy + cmd.ensure_finalized() + + included = use_fuzzy or not fuzzy + assert cmd.run() == int(included) + assert ("unknown named placeholder 'other'" in caplog.text) == included + + with mo_file.open('rb') as infile: + translations = GNUTranslations(infile) + assert translations.gettext('Hello %(name)s') == ( + 'Hello %(other)s' if included else 'Hello %(name)s' + ) + assert translations.gettext('Goodbye') == 'Bye'