Problem/Motivation

When a .po translation file contains plural translations with empty strings for both msgstr[0] and msgstr[1], Drupal incorrectly treats this as a valid translation.

For example, the following .po entry is treated as valid:

msgid "@count comment"
msgid_plural "@count comments"
msgstr[0] ""
msgstr[1] ""

Internally, Drupal uses a special delimiter (\x03, defined in PoItem::DELIMITER) to separate plural forms. So even if both strings are empty, Drupal stores the translation as "\x03", which is not technically empty. This leads to incorrect fallback behavior: the system thinks a translation exists and uses it, resulting in an empty string rendered to the user.

This affects output from formatPlural(), Drupal.formatPlural(), is copied into configuration translation when applicable and is considered as "translated" when the admin filters to translated strings on the interface translation UI.

Unfortunately the Configuration Translation module also has a similar bug with its own UI: #3632104: Configuration translation saves a delimiter-only string when all plural variants are empty, instead of dropping it

Proposed resolution

When importing translations, do not consider an empty set of plural values as a translation, treat this as empty. The locale UI already does the same for when all variants are submitted as empty.

Remaining tasks

  • Review

User interface changes

None.

Introduced terminology

None.

API changes

None.

Data model changes

None.

Release notes snippet

None.

Issue fork drupal-3529265

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

qrinaldi created an issue. See original summary.

qrinaldi’s picture

needs-review-queue-bot’s picture

Status: Needs review » Needs work

The Needs Review Queue Bot tested this issue.

While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

qrinaldi’s picture

qrinaldi’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Think something went wrong with the MR

qrinaldi’s picture

Status: Needs work » Needs review

I'm very sorry about that. This is fixed.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks, think we will need a test case to show how this is is an error.

qrinaldi’s picture

Status: Needs work » Needs review

I've added a functional test to ensure the fallback behavior works correctly when plural translations are empty. The test is based on existing ones in the Locale module, particularly the pattern used in similar plural-related tests.

This is my first contribution to Drupal core — any feedback is welcome!

quietone’s picture

Version: 11.1.x-dev » 11.x-dev

If this problem was discovered on a version of Drupal that is not 11.x, add that information in the issue summary and leave the version at 11.x. In Drupal core changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies. Also mentioned on the version section of the list of issue fields documentation.

qrinaldi’s picture

Issue summary: View changes

I've added to the issue summary that the bug was discovered on Drupal 11.1.6, as suggested.

smustgrave’s picture

Status: Needs review » Needs work

Instead of a new file can we extend LocalePluralFormatTest

qrinaldi’s picture

[duplicate]

qrinaldi’s picture

Status: Needs work » Needs review

Just edited last commit, extending LocalePluralFormatTest

smustgrave’s picture

#13 still applied but will leave in review.

qrinaldi’s picture

What do you mean ? I've extended LocalePluralFormatTest as requested.
EDIT: got it
I moved the code into LocalePluralFormatTest

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -format plural, -plural text, -TranslatableMarkup, -Needs tests

Left some comments on the MR.

Also cleaned up the tags some, most of those aren't in use by core.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

gábor hojtsy’s picture

Issue tags: +Plural handling

Adding to the growing pool of plural handling issues :)

gábor hojtsy’s picture

The UI does not allow this situation to happen with translation input because it deletes the translation when all inputs for a plural are empty. I found config translation doing the same as .po import and opened #3632104: Configuration translation saves a delimiter-only string when all plural variants are empty, instead of dropping it. I think the right course of action is to not save the empty string in the .po import not to try to catch the issue in the runtime though, so the translation database is clean instead of having bugos items.

gábor hojtsy’s picture

Following my hunch about needing to fix in import, I used my LLM to list the places the existing fix would not solve. If we are to only filter this on the PHP translation runtime side, these will still be a problem:

  • JavaScript: LocaleJs::rebuild() writes it into the JavaScript translation file, and Drupal.formatPlural() returns an empty string.
  • Configuration: LocaleConfigManager::translateString() copies it into configuration translations, because TranslationString::isTranslation() is TRUE for "\x03". A shipped Views plural format would then show nothing.
  • Translation UI: the “Only translated strings” filter lists it as translated.

Therefore looking into an LLM assisted fix for the import instead of the display.

gábor hojtsy’s picture

Title: formatPlural() returns empty string when translation msgstr values are empty, due to delimiter presence » Plural .po import saves as delimiters-only when the translations are empty instead of not saving anything
gábor hojtsy’s picture

Issue summary: View changes

Updated issue summary with recommended solution and formatting of the body and cross-linking to teh config translation bug.

gábor hojtsy’s picture

Issue summary: View changes

Don't think release notes snippet is worth it.

gábor hojtsy’s picture

Status: Needs work » Needs review
gábor hojtsy’s picture

Issue summary: View changes

Added a post update function too to remove the wrong "translations". Including tests.

So I think this is complete.

gábor hojtsy’s picture

Issue summary: View changes
gábor hojtsy’s picture

Status: Needs review » Postponed (maintainer needs more info)

Actually this has behavioural consequences for #3632104: Configuration translation saves a delimiter-only string when all plural variants are empty, instead of dropping it, so marked this postponed too. We need to comprehensively figure out how empty customized translations are supposed to work, if that has merit at all in config translation, etc.