When using the StringTranslationTrait in a service why should you inject the 'string_translation' service aswell? I was reading the code comments in the StringTranslationTrait trait and saw the following:


  If the class is capable of injecting services from the container, it
  should inject the 'string_translation' service and assign it to
  $this->stringTranslation.


So I created a simple service, used the StringTranslationTrait, injected the string_translation service and assigned it to $this-&gt;stringTranslation. I found that when I don't inject the string_translation service the $this-&gt;t() function still works as I expected.

Why should one inject said service into his own service? Is it good practice or are there other benefits such as speed? The comments in the StringTranslationTrait file offer no explanation.

My service for completeness:

&lt;?php

namespace Drupal\wow\Service;

use Drupal\Core\Url;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Class RedirectService.
 *
 * @package Drupal\wow\Service
 */
class RedirectService {

  use StringTranslationTrait;

  /**
   * The redirect object.
   *
   * @var \Symfony\Component\HttpFoundation\RedirectResponse
   */
  private $redirectResponse;

  /**
   * RedirectService constructor.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $stringTranslation
   *   The string translation service.
   */
  public function __construct(TranslationInterface $stringTranslation) {
    $this-&gt;stringTranslation = $stringTranslation;
  }

  /**
   * Sets the redirect.
   *
   * @param string $routeName
   *   Drupal route name to redirect to.
   * @param string $message
   *   Message to show after redirecting. If not provided no message will be
   *   shown.
   * @param string $messageType
   *   Message type of message shown after redirecting.
   */
  public function setRedirect($routeName, $message = NULL, $messageType = 'status') {
    $uri = Url::fromRoute($routeName);

    if ($message) {
      drupal_set_message($this-&gt;t($message), $messageType);
    }

    $this-&gt;redirectResponse = new RedirectResponse($uri-&gt;toString());
  }

  /**
   * Gets the redirect object.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   The redirect object.
   */
  public function getRedirect() {
    return $this-&gt;redirectResponse;
  }

  /**
   * Returns the response object.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response object.
   */
  public function send() {
    return $this-&gt;redirectResponse-&gt;send();
  }

}
If you didn't inject the string translation service into $this-&gt;stringTranslation, the trait would get it from the static \Drupal wrapper

  /**
   * Gets the string translation service.
   *
   * @return \Drupal\Core\StringTranslation\TranslationInterface
   *   The string translation service.
   */
  protected function getStringTranslation() {
    if (!$this-&gt;stringTranslation) {
      $this-&gt;stringTranslation = \Drupal::service('string_translation');
    }

    return $this-&gt;stringTranslation;
  }


and this should be avoided in OOP code. The main reason for this is to be able to inject mock-up services for code tests.