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
A number times in /src we use the `StringTranslationTrait` but don't call `setStringTranslation()` or have a dependency on the translation service.
Proposed resolution
We should always have a dependency on the translation service and not rely on `\Drupal\Core\StringTranslation\StringTranslationTrait::getStringTranslation` to get the service unless we absolutely have to.
Issue fork automatic_updates-3243057
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
phenaproximaRather than a direct dependency on the string translation service, I propose we take advantage of the container's
calls
directive for services that use StringTranslationTrait. For example:This will instantiate the service, then immediately call
setStringTranslation()
on it, passing the string translation service. It reduces the amount of boilerplate we need to write.Comment #3
tedbowhmmm. why do we have to do this different from core’s current practice? setStringTranslation is not found anywhere in core in *.services.yml files.
@string_translation is found many times in *.services.yml files.
maybe core should change its practices but for anybody new trying to understand the core codebase I think there is immense value in things being more uniform.
somebody who understands how this works for every other service in core that needs translation is going to have to think just a little bit harder to understand our module
Comment #6
kunal.sachdev CreditAttribution: kunal.sachdev commentedComment #7
tedbowLooks good!
Comment #9
tedbowComment #10
kunal.sachdev CreditAttribution: kunal.sachdev at Acquia commented