Skip to content

concat: refuse to combine catalogs with different Plural-Forms - #1507

Open
tony1233200 wants to merge 1 commit into
python-babel:masterfrom
tony1233200:fix/1506-concat-plural-forms-mismatch
Open

tony1233200 wants to merge 1 commit into
python-babel:masterfrom
tony1233200:fix/1506-concat-plural-forms-mismatch

Conversation

@tony1233200

Copy link
Copy Markdown

Fixes #1506

Combining catalogs whose Plural-Forms headers disagree silently misplaces plural translations: the same form index means something different under each declaration (e.g. a legacy two-form French catalog vs. a current three-form one). pybabel concat now refuses with an OptionError instead of writing a broken catalog. Catalogs without a locale and without a Plural-Forms header (e.g. POT templates) are not compared.

Repro (before fix): pybabel concat of the two French catalogs from #1506 produced a combined catalog with the three-form header but only two msgstr forms for ticket. After fix it fails fast with OptionError: input catalogs disagree on Plural-Forms ... and writes no output.

Tests: added test_concat_refuses_mismatched_plural_forms and test_concat_accepts_matching_plural_forms. tests/messages/: 525 passed, 1 skipped; ruff check clean.

Combining catalogs whose Plural-Forms headers disagree silently
misplaces plural translations: the same form index means something
different under each declaration (e.g. a legacy two-form French
catalog vs. a current three-form one). Refuse with an OptionError
instead of writing a broken catalog.

Fixes python-babel#1506

@anlonely anlonely left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested pybabel concat at 4bb3926 with small PO files. It correctly refused the two-form/three-form mismatch. I found two other cases worth covering below. OpenAI Codex ran the reproductions and helped draft this review.

has_header = any(key.lower() == 'plural-forms' for key, _ in template.mime_headers)
if template.locale is None and not has_header:
continue
forms = ' '.join(template.plural_forms.split())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces a rejection for two compatible catalogs. I used nplurals=2; plural=(n != 1); in one PO and nplurals=2; plural=(n!=1); in the other, with different message IDs. master combines them, but this raises OptionError because joining with a space still distinguishes optional whitespace from no whitespace. Comparing the count and expression with whitespace removed for the comparison only accepted this pair locally and still rejected the two-form/three-form case.

for filename, template in templates:
has_header = any(key.lower() == 'plural-forms' for key, _ in template.mime_headers)
if template.locale is None and not has_header:
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s a remaining data-loss case when neither input has a Language header. I put an untranslated file/files in template.pot with no plural rule, then the same ID in translated.po with Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n==2 ? 1 : 2); and translations one, two, many. pybabel concat -o out.po template.pot translated.po succeeds but drops msgstr[2] and the rule. Reversing the inputs keeps all three. This also happens on master. The template is skipped here but still wins the header fallback below. Using the first input with a plural declaration as the fallback when no locale is available kept all three forms in both orders in my local check.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Combining catalogs with different Plural-Forms silently loses plural translations

3 participants