API page: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Language%...
The documentation on this page refers to a global "formatPlural" function. However, there is no global formatPlural or format_plural function anymore. This was moved to the translation service and then removed. #2366539: Remove format_plural().
This block of text needs to be updated:
Any time UI text is displayed using PHP code, it should be passed through either the global t() function or a t() method on the class. If it involves plurals, it should be passed through either the global formatPlural() function or a formatPlural() method on the class. Use \Drupal\Core\StringTranslation\StringTranslationTrait to get these methods into a class.
As does the code example at the bottom of the documentation.
Related change record #2173787: format_plural() has moved to translation service as formatPlural() and format_interval() has moved to date service as formatInterval()
| Task | Novice task? | Contributor instructions | Complete? |
|---|---|---|---|
| Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards | Novice | Instructions |
| Comment | File | Size | Author |
|---|---|---|---|
| #37 | interdiff-2668008-31-37.txt | 795 bytes | dimaro |
| #37 | internationalization-2668008-37.patch | 784 bytes | dimaro |
| #31 | internationalization-2668008-31.patch | 671 bytes | kannan@kiluvai.com |
| #19 | interdiff-2668008-15-17.txt | 1.15 KB | kannan@kiluvai.com |
| #17 | internationalization-2668008-17.patch | 881 bytes | kannan@kiluvai.com |
Comments
Comment #2
jhodgdonStringTranslationTrait::formatPlural() still exists:
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!StringTranslation...
So the advice to use the formatPlural() method if it exists on a class, and to use the trait to get this method, is still good.
However, you're right that the "global formatPlural() function" doesn't exist. In a global scope, the correct advice would be to use PluralTranslatableMarkup::createFromTranslatedString().
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!StringTranslation...
Should be a good novice project to update that part of the docs.
Comment #3
felribeiro commentedComment #4
jhodgdonThanks! Almost there, but it needs a bit of work:
This needs to have the namespace, starting with \, on the PluralTranslatableMarkup class.
The previous wording that this patch replaces was better, regarding what to do on a class. Can we get that back please?
Comment #5
rakesh.gectcr@jhodgdon
I worked on your above comments.
But i need your help for following
with namespace it is crossing 80 characters, what can be done here?
Comment #6
jhodgdonJust put the too-long thing on one line. We make exceptions to line lengths for namespaces being too long in docs, since there is no solution, but it needs to be by itself on a line so that it goes over by the minimal amount. Thanks!
So in this patch:
the word "use" should be removed on this line. Then just leave it as it is, and do not worry about the 80-character limit.
We need a space after the word "Use" here.
Comment #7
rakesh.gectcr@jhodgdon
I made the changes according to #6.
Comment #9
dimaro commentedThe last patch did not apply.
Rerolled #7.
I thought it would be more correct to link the following lines into one:
@jhodgdon This change would be right?
Comment #10
eojthebraveI find the phrasing "in a global scope" to be a little weird. It threw me off. I think maybe just saying, "involves plurals, it should be passed through either the global \Drupal\Core\StringTranslation\PluralTranslatableMarkup::createFromTranslatedString(), or ..." might be a little clearer? I think either works, but for some reason the existing one trips me up a little bit.
Comment #11
jhodgdonNeeds work for #10.
Also these three lines still need to be rewrapped so that all docs lines get as close to 80 characters as possible. (Of course, rewrap after the rewording!)
Comment #12
dimaro commentedApply changes mentioned on #10, #11.
@jhodgdon, I have a question.
We need to work to get as close to 80 characters throughout the document? Or only in this block?
I had doubts about this.
Thanks!
Comment #13
xjmLooking at this issue, the next step is to review the patch and address @dimaro's comments above. It looks like a good novice task according to https://www.drupal.org/core-mentoring/novice-tasks.
Comment #14
jhodgdonAll documentation blocks and // comments should be wrapped to go as close to 80 characters as possible, without going over. In some cases (as discussed above, for instance), we allow lines to go over 80 charactres. But probably you shouldn't go fix anything else in this file on this patch, just the area being worked on.
So... it's still not fixed:
The wording looks good now to me, but these lines are still not wrapped to get as close to 80 characters as possible in each line. Please fix this.
Comment #15
dimaro commentedThanks @jhodgdon.
Would be correct now?
Comment #16
kannan@kiluvai.com commentedComment #17
kannan@kiluvai.com commented@dimaro
adjusted patch as suggested by @jhodgdon in #14
Comment #18
kannan@kiluvai.com commentedComment #19
kannan@kiluvai.com commentedAttaching interdiff file
Comment #20
mobaid commentedI have taken a look at #17 and have affirmed that :
\Drupal\Core\StringTranslation\PluralTranslatableMarkup::createFromTranslatedString()cannot be shortened to be restricted within the 80chars as a single line.Converting this to RTBC.
Comment #21
mobaid commentedComment #24
xjm@webchick committed this issue LIVE at DrupalCon Asia!
I cherry-picked the commit to 8.0.x since we had some git fun onstage. ;)
Comment #25
xjm(And adding in the issue credits!)
Comment #26
eojthebraveI think this might still need some work. Looking into this a little more I have a few questions at least.
First of all, should we be using \Drupal::translation()->formatPlural() instead of PluralTranslatableMarkup::createFromTranslatedString()? Looking at the code in core the former is used far more often than the latter.
Second, the patch that got committed only addresses part of the issue. The text of the documentation is updated, but the code example at the bottom of the @docblock is not. This is what is there currently:
But should probably be more like:
Or using PluralTranslatableMarkup::createFromTranslatedString(), whichever we decided is the one to recommend here.
Sorry I didn't catch these things sooner. I'm going to re-open this issue, but feel free to close this and open a new one if that's more appropriate.
Comment #27
jhodgdonUh oh, good catch on the code block! We should definitely fix that.
Regarding the first question, yes createFromTranslatedString() is the Best Way (TM) to do this, even though it is not the most common in the code base right now, because it was introduced relatively recently.
Comment #28
kannan@kiluvai.com commentedHi @jhodgdon and @eojthebrave
here is modified code sample picked from https://api.drupal.org/api/drupal/core%21modules%21locale%21src%21Tests%...
can you confirm, if so i will create patch with this.
Comment #29
xjmHi @kannan@kiluvai.com,
In general, it's best to post the patch first and then contributors can review it in context. Posting patches that are not perfect is part of the process. :)
However, in this case, part of that code snippet looks incorrect. I'd suggest not using a test for the API itself as a model. It would be better to look for use of
formatPlural()in module code to use as an example.Comment #30
jhodgdonEcho what xjm said. ... So, a more normal way to use the PluralTranslatableMarkup object is to do a call like this:
Comment #31
kannan@kiluvai.com commentedCreated patch for this and attached.
I have picked the code sample from https://api.drupal.org/api/drupal/core%21includes%21common.inc/8 line no 253
formatPlural function using the PluralTranslatableMarkup class, so i thought formatPlural will be right choice to use.
Comment #32
kannan@kiluvai.com commentedComment #33
jhodgdonUm, this latest patch has lost the changes from #17 that we still need (well, we need it but we need it to be fixed).
Comment #34
dimaro commented@jhodgdon #17 was commited no?
I try to join #17 and #31, but #17 is already in 8.0.x.
Comment #35
jhodgdonOh, you're right, sorry!
OK. Looking at what we currently have on
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Language!language...
vs. the patch... you are right, the patch is complete as far as what needs to be fixed.
But
Probably we should keep the same example that was in this code before: 1 something / @count somethings, rather than changing to cows.
Comment #36
jhodgdonOh. Also there appears to be an @see format_plural() at the bottom of this doc block. That should also be fixed, or just removed.
Comment #37
dimaro commentedApply changes mentioned on #35 and #36.
On 8.0.x there was no word 'cow/s', only in #31 (See interdiff).
I removed @see format_plural()
Would be right?
Comment #38
jhodgdonThanks! That looks right to me.
Comment #42
catchCommitted/pushed to 8.2.x and cherry-picked to 8.1.x and 8.0.x. Thanks!