Updated: Comment #7
Problem/Motivation
Follow-up from #2111349: Move format_plural to the string translation service and format_interval to the date service.. Implementation of #2111349: Move format_plural to the string translation service and format_interval to the date service. is not applied side-wide.
Proposed resolution
Implement #2111349: Move format_plural to the string translation service and format_interval to the date service. by
1. Inject translation service in Base classes (ControllerBase, FormBase and PluginBase) and create a helper method.
2. Use static \Drupal::translation()->formatPlural() call on procedural and test methods.
Remaining tasks
Patch - In progress
User interface changes
N/A
API changes
N/A
Comment | File | Size | Author |
---|---|---|---|
#51 | interdiff-2149195-48-51.txt | 2.23 KB | herom |
#51 | format_plural-2149195_51.patch | 29.36 KB | herom |
#48 | format_plural-2149195_48.patch | 31.59 KB | herom |
#39 | format_plural-2149195.patch | 78.28 KB | dawehner |
Comments
Comment #1
vijaycs85Comment #2
vijaycs85Initial patch...
Comment #3
vijaycs85Comment #4
penyaskitoShouldn't we inject the translation service?
Comment #5
dawehnerMy thoughts!
Comment #6
tim.plunkettWe should consider adding it to FormBase.
Comment #7
Gábor HojtsyExactly, we should not just simply replace the global function calls with global static method calls.
Comment #8
vijaycs85Ok, injected and created helpers FormBase, ControllerBase and PluginBase and all classes extending them just use by calling $this->formatPlural(). Tests and procedural functions using Drupal::translation()->formatPlural()
Comment #9
vijaycs85Updating issue summary with more details.
Comment #11
penyaskitoComment #13
penyaskitoI must do the same in FormBase and PluginBase...
Comment #14
Gábor HojtsyBTW we have #2151527: Support for formatPlural() in PHP as format_plural() is deprecated open in potx to track this change in the parser.
Comment #15
vijaycs85arrrgh!!! sorry for that copy-paste error and thanks for fixing @penyaskito. So we got below instance of current code base which we may still need to keep:
Comment #16
penyaskitoLet's try to change it.
Comment #17
ParisLiakos CreditAttribution: ParisLiakos commentedi dont see any phpunit tests for reviews, so i guess the correct tag is this one
Comment #18
ParisLiakos CreditAttribution: ParisLiakos commentedAnd since i am here
we are supposed to use $this->container->get() inside simpletests instead of \Drupal
Also a reroll
Comment #19
penyaskitoRerolled and used $this->container->get('string_translation')->formatPlural() in tests. Thanks for the review, Paris!
Comment #20
tim.plunkett#2087751: [policy, no patch] Stop using $this->container in web tests :(
Comment #21
penyaskitoPlain reroll of #16 per #20.
Comment #23
penyaskito21: 2149195-formatPlural-21.patch queued for re-testing.
Comment #24
penyaskito21: 2149195-formatPlural-21.patch queued for re-testing.
Comment #27
vijaycs85Re-roll...
Comment #28
penyaskitoComment #29
herom CreditAttribution: herom commentedComment #30
dawehnerThere aren't any out of scope changes and the patch is green.
Comment #31
webchickHm. We've gone from "always call
format_plural()
" to sometimes call$this->formatPlural()
and other times call\Drupal::translation()->formatPlural()
. This does not seem to be a huge DX win for me. :\t() is a bit different because in either case you're calling
(whatever OO crap)->t()
as opposed to sometimes (but not always) needing to know it's buried underneath a top-level "translation()" object instead.In other words, I think we should either make it a top-level method on Drupal, or we should make the base classes mock translation() as well, so calls to this can be consistent throughout the code base.
Comment #32
vijaycs85Ok we have injected to OO so $this->formatPlural works whereas procedural code we have no option except to use \Drupal::translation()->formatPlural(). IMHO, this is only better way until get all OO.
reg: t() we haven't done t() for procedural. So t() is 1 step behind what we have for formatPlural().- We have the implementation in place, but not deprecated t() calls.ideally we should use \Drupal::translation()->translate() instead of t().
Comment #33
webchickWhy..? That is exposing super unimportant implementation details to someone who just. wants. to. freaking. print. a. string. :)
Comment #34
dawehnerWait for traits, which will add t()\formatPlural() to every possible class.
Comment #35
tim.plunkettNope, because that would break localize.d.o, which is the entire reason t() exists.
Yep, this. See #2079797: Provide a trait for $this->t() and $this->formatPlural(), we need an equivalent.
Comment #36
webchickYeah, traits are where we ultimately want to go, but there are a pile of dependencies between here and there, starting with #2135199-35: Provide php 5.4 testing on testbots for D8 code without breaking everything else.
My concerns for the purposes of committing this particular patch can be addressed by merely adding a formatPlural() method straight onto the Drupal class, so it retains parity with t(). If we can't or won't do that for some reason, I'd love to understand why, and also how we plan to explain to module developers why sometimes they need an extra ->translate() indirection prior to calling the function they have in mind.
Comment #37
sunComment #38
dawehnerWe don't have a t() method on the \Drupal class as well for a good reason. We need to encourage people to write proper code/inject dependencies instead of writing sort of procedural code. People will understand that they need a method on a class or a dependency before they can do anything.
Comment #39
dawehnerJust in case, here is also a reroll.
Comment #40
naveko CreditAttribution: naveko commentedReworked the latest patch, leaving out the static method call \Drupal::translation()->formatPlural(). There is no agreement on whether to use the static method instead of format_plural, but on the implementation of formatPlural in the Base Classes there is.
Comment #42
rych CreditAttribution: rych commentedComment #44
Gábor HojtsyThe forum block tests fail. Is that using views already? :) No direct forum changes are in this patch.
Comment #45
Gábor Hojtsy42: format_plural-2149195_42.patch queued for re-testing.
Comment #47
Gábor HojtsyRemove from sprint.
Comment #48
herom CreditAttribution: herom commentedreroll. let's see what's failing, then I'll try fixing it.
Comment #49
herom CreditAttribution: herom commentedComment #51
herom CreditAttribution: herom commentedreverted a couple of
$this->formatPlural()
s in procedural code.Comment #52
Gábor HojtsyThis now sets up formatPlural() on base classes like t() and uses it as appropriate. This looks sane from a DX point of view so there is no difference between t() and format_plural() for calling a local method vs. a global function.
Comment #53
webchickLooks like this is covered in the change record at https://drupal.org/node/2173787
Committed and pushed to 8.x. Thanks!
Comment #54
Gábor HojtsyYay, thanks all!