Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion babel/messages/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions tests/messages/frontend/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'