Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
This issue has a non-test scope, which means don't convert test classes.
Classes which use t()
should convert their usage to an injected translation service.
Proposed resolution
t()
usages inside static class methods can't be injected, so leave those.
Other usages should ensure that the class uses Drupal\Core\StringTranslation\StringTranslationTrait
, or otherwise has an implementation of $this->t()
.
Calls to t()
in non-static methods should be changed to $this->t()
.
Remaining tasks
Eventually perform IoC for service traits such as StringTranslationTrait: #2733703: [plan] Service traits should require IoC injection
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#45 | Replace_of_t-2806983-45.patch | 3.16 KB | manishsaharan |
#30 | interdiff-2806983-26-30.txt | 327 bytes | minakshiPh |
#30 | fix-issue-2806983-30.patch | 2.75 KB | minakshiPh |
#26 | interdiff-2806983-22-26.txt | 586 bytes | minakshiPh |
#26 | fix-issue-2806983-26.patch | 3.31 KB | minakshiPh |
Comments
Comment #2
kunalkursija CreditAttribution: kunalkursija at Iksula, Blisstering Solutions commentedAdding Patch.
Comment #3
minakshiPh CreditAttribution: minakshiPh at Iksula commentedComment #5
aditya.n CreditAttribution: aditya.n as a volunteer and at gai Technologies Pvt Ltd for gai Technologies Pvt Ltd commentedComment #6
aditya.n CreditAttribution: aditya.n as a volunteer and at gai Technologies Pvt Ltd for gai Technologies Pvt Ltd commentedAdding the patch for the issue
Comment #8
kunalkursija CreditAttribution: kunalkursija at Iksula, Blisstering Solutions commented@androiditya
Static functions can be used without instantiation, Hence you cannot use "$this" inside static functions.
Comment #9
naveenvalechaThis will fix the errors
Comment #10
naveenvalechaThis is a BC break, but as the module is in alpha then who cares ;)
Comment #11
amit.drupal CreditAttribution: amit.drupal as a volunteer and at gai Technologies Pvt Ltd for gai Technologies Pvt Ltd commentedComment #12
Mile23Unfortunately, hooks don't have a
$this
, much less$this->t()
. So either leave the globalt()
or use$translation = \Drupal::translation()
to get the service, and then call$translation->t()
.StringTranslationTrait
has asetStringTranslation()
method we should use during the constructor.Comment #13
amit.drupal CreditAttribution: amit.drupal as a volunteer and at gai Technologies Pvt Ltd for gai Technologies Pvt Ltd commentedComment #14
amit.drupal CreditAttribution: amit.drupal as a volunteer and at gai Technologies Pvt Ltd for gai Technologies Pvt Ltd commentedComment #15
amit.drupal CreditAttribution: amit.drupal as a volunteer and at gai Technologies Pvt Ltd for gai Technologies Pvt Ltd commented@Mile23 Thanks for review code.
Comment #17
amit.drupal CreditAttribution: amit.drupal as a volunteer and at gai Technologies Pvt Ltd for gai Technologies Pvt Ltd commented@naveenvalecha Please Suggest me issues in 2806983-11.patch
Comment #18
naveenvalecha#11, Please do test on local before posting, Did you even test the patch on d.o.
Unreleated changes.
#12.2
yup nice spot :)
Comment #19
amit.drupal CreditAttribution: amit.drupal as a volunteer and at gai Technologies Pvt Ltd for gai Technologies Pvt Ltd commented@naveenvalecha Thanks
Next time improve my coding.
Comment #20
minakshiPh CreditAttribution: minakshiPh at Iksula commentedComment #21
keshavv CreditAttribution: keshavv at gai Technologies Pvt Ltd for gai Technologies Pvt Ltd commentedHere is the patch please review
Comment #22
minakshiPh CreditAttribution: minakshiPh at Iksula commentedcorrected the patch as mentioned in #12
Kindly review.
Thanks!
Comment #24
Mile23Thanks. :-)
The trait adds
setStringTranslation()
to$this
, so it would be$this->setStringTranslation($string_translation);
Comment #25
minakshiPh CreditAttribution: minakshiPh at Iksula commentedComment #26
minakshiPh CreditAttribution: minakshiPh at Iksula commentedHi @Mile23,
Thanks for reviewing my patch.
Have corrected the minor change as mentioned in #24.
Kindly review.
Thanks!
Comment #27
naveenvalechaThis will make the test fail b/c the $this->t() function will not be available here.
Comment #28
tedbow@minakshiPh Settings needs work because of #27
Comment #29
minakshiPh CreditAttribution: minakshiPh at Iksula commentedComment #30
minakshiPh CreditAttribution: minakshiPh at Iksula commentedHi @tedbow,
Change mentioned in #27 is done and attached the new patch.
Kindly review.
Thanks!
Comment #31
pmchristensen CreditAttribution: pmchristensen commentedLooking really good - reviewed and tested. It should perhaps be considered to do something about the skipping tests?
Comment #33
xjmThank you for your work on cleaning up Drupal core's use of deprecated APIs!
In order to deprecate APIs in a maintainable way, converting deprecated uses should be replaced across all of core for a given kind of usage, rather than in individual modules or files. Such issues should also always be part of an overall plan to ensure all usages are removed, rather than standalone patches. Such a plan may already exist for
t()
so this issue should probably be closed in favor of that.For background information on why we usually will not commit cleanups that aren't scoped in that way, see the core issue scope guidelines. See #2575081: [policy, no patch] Use E_USER_DEPRECATED in Drupal 8 minor releases for more information on how we plan to manage deprecations in the future.
Contributing to the overall plan above will help ensure that your cleanups for core's deprecated code improve core in a maintainable and minimally disruptive way.
For this issue, we should start by replacing non-test
t()
usages in all classes where the translation trait or service is already available. This can be done with a script. We would then have a followup to inject the service in classes where it is not already available and convert usages there. Finally, we should have a separate issue to discuss the use oft()
in tests. We probably should close this issue as a duplicate, but setting to needs work for now so that contributors see this information. Thanks!You might want to help contribute to this issue instead: #2549805: Remove all usage of FormattableMarkup in tests apart from explicit tests of that API
Comment #34
ZeiP CreditAttribution: ZeiP as a volunteer and at Citrus Solutions Oy commentedComment #35
tedbowClosing as per @xjm comments in #33
If anybody knows of such as issue please link to it from here.
Comment #36
Mile23Can't be done with a script. Someone is welcome to prove me wrong, however.
I don't think the larger-scope issue exists since I couldn't find it. I'm re-opening and re-scoping here, since we already have a patch here.
Comment #45
manishsaharan CreditAttribution: manishsaharan commentedFixed this with 9.2.x
Comment #50
smustgrave CreditAttribution: smustgrave at Mobomo commentedClosing as duplicate of #3122912: t() calls should be avoided , use $this->t() instead in Form Builders