Problem/Motivation
As reported in #2545730-2: Misuse of formatPlural() in Numeric field prefix/suffix \Drupal\Core\Validation\DrupalTranslator::transChoice() uses \Drupal::translation()->formatPlural() in what looks like an incorrect manner. Here's what it's doing:
// Violation messages can separated singular and plural versions by "|".
$ids = explode('|', $id);
if (!isset($ids[1])) {
throw new \InvalidArgumentException(sprintf('The message "%s" cannot be pluralized, because it is missing a plural (e.g. "There is one apple|There are @count apples").', $id));
}
return \Drupal::translation()->formatPlural($number, $ids[0], $ids[1], $this->processParameters($parameters), $this->getOptions($domain, $locale));
}
However, if transChoice() is used with messages that are defined on a constraint class, that is already supported by potx, see #1903362: Support for Drupal 8 validation constraint messages. We should document this usage.
Proposed resolution
Document it.
Remaining tasks
Commit.
User interface changes
None.
API changes
None.
Data model changes
None.
Brief beta eval: This is just adding a code comment (documentation). No disruption. Can be committed any time.
Comment | File | Size | Author |
---|---|---|---|
#8 | 2552867-document-nonstandard-format-plural.patch | 1.43 KB | jhodgdon |
Comments
Comment #1
Anonymous (not verified) CreditAttribution: Anonymous at XIO commentedpjonckiere created an issue. See original summary.
Comment #2
Anonymous (not verified) CreditAttribution: Anonymous at XIO commentedComment #3
Anonymous (not verified) CreditAttribution: Anonymous at XIO commentedComment #4
jhodgdonAdding details from the other issue to the summary here.
Comment #5
Gábor HojtsyThe code does not actually use that in formatPlural()?
Comment #6
jhodgdonHere's the code:
So the source string is $id and it indeed has singular/plural forms separated by | and is passing those into formatPlural().
Comment #7
Gábor HojtsyI see, so the concern is the extractability of the source string first. Let's see, #1903362: Support for Drupal 8 validation constraint messages will extract the | separated string in potx for constraint messages. This seem to be only used for constraint messages, right? Or is it used for other things as well?
Comment #8
jhodgdonAh, so that is very interesting. In that case it looks like DrupalTranslator::transChoice() should be OK to use the | character in its constraint messages and call formatPlural(), because we've specially modified the POTX extractor to make this work.
So for this issue I think we should just add a comment to this code so that people don't try to use it elsewhere.
Here's a patch.
Comment #9
Gábor HojtsySuperb. Updated the summary too :)
Comment #10
jhodgdonAdding beta eval and now this is just documentation (wasn't really field system anyway).
Comment #13
Gábor HojtsyComment #14
alexpottCommitted db342cb and pushed to 8.0.x. Thanks!
Comment #16
Gábor HojtsyThanks!