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.
| Comment | File | Size | Author |
|---|
Issue fork drupal-3529265
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
Comment #2
qrinaldi commentedComment #3
needs-review-queue-bot commentedThe 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.)
Comment #5
qrinaldi commentedComment #6
qrinaldi commentedComment #7
smustgrave commentedThink something went wrong with the MR
Comment #8
qrinaldi commentedI'm very sorry about that. This is fixed.
Comment #9
smustgrave commentedThanks, think we will need a test case to show how this is is an error.
Comment #10
qrinaldi commentedI'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!
Comment #11
quietone commentedIf 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.
Comment #12
qrinaldi commentedI've added to the issue summary that the bug was discovered on Drupal 11.1.6, as suggested.
Comment #13
smustgrave commentedInstead of a new file can we extend LocalePluralFormatTest
Comment #14
qrinaldi commented[duplicate]
Comment #15
qrinaldi commentedJust edited last commit, extending LocalePluralFormatTest
Comment #16
smustgrave commented#13 still applied but will leave in review.
Comment #17
qrinaldi commentedWhat do you mean ? I've extended LocalePluralFormatTest as requested.
EDIT: got it
I moved the code into LocalePluralFormatTest
Comment #18
smustgrave commentedLeft some comments on the MR.
Also cleaned up the tags some, most of those aren't in use by core.
Comment #20
gábor hojtsyAdding to the growing pool of plural handling issues :)
Comment #21
gábor hojtsyThe 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.
Comment #22
gábor hojtsyFollowing 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:
LocaleJs::rebuild()writes it into the JavaScript translation file, andDrupal.formatPlural()returns an empty string.LocaleConfigManager::translateString()copies it into configuration translations, becauseTranslationString::isTranslation()is TRUE for "\x03". A shipped Views plural format would then show nothing.Therefore looking into an LLM assisted fix for the import instead of the display.
Comment #23
gábor hojtsyComment #24
gábor hojtsyUpdated issue summary with recommended solution and formatting of the body and cross-linking to teh config translation bug.
Comment #25
gábor hojtsyDon't think release notes snippet is worth it.
Comment #26
gábor hojtsyComment #27
gábor hojtsyAdded a post update function too to remove the wrong "translations". Including tests.
So I think this is complete.
Comment #28
gábor hojtsyComment #29
gábor hojtsyActually 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.