Repository navigation
concat: refuse to combine catalogs with different Plural-Forms - #1507
tony1233200 wants to merge 1 commit into
Conversation
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
| 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()) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
Fixes #1506
Combining catalogs whose
Plural-Formsheaders 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 concatnow refuses with anOptionErrorinstead of writing a broken catalog. Catalogs without a locale and without aPlural-Formsheader (e.g. POT templates) are not compared.Repro (before fix):
pybabel concatof the two French catalogs from #1506 produced a combined catalog with the three-form header but only twomsgstrforms forticket. After fix it fails fast withOptionError: input catalogs disagree on Plural-Forms ...and writes no output.Tests: added
test_concat_refuses_mismatched_plural_formsandtest_concat_accepts_matching_plural_forms.tests/messages/: 525 passed, 1 skipped;ruff checkclean.