How to use t() function in controller?

by Jiang Zhan   Last Updated May 15, 2019 23:07 PM

I understand Drupal 8 ControllerBase implements StringTranslationTrait so we can use $this->t() in Controller class. But I found in Drupal 8 examples code, they setStringTranslation to use t() function. Here is the code.

public static function create(ContainerInterface $container) {
    $controller = new static(
      $container->get('database')
    );
    $controller->setStringTranslation($container->get('string_translation'));
    return $controller;
  }

The link for their module. https://git.drupalcode.org/project/examples/blob/8.x-1.x/tablesort_example/src/Controller/TableSortExampleController.php

My question is why setStringTranslation when you can just use t() function in the controller.

Tags : 8 i18n-l10n


Related Questions


Updated May 16, 2016 08:03 AM

Updated June 28, 2016 08:03 AM

Updated August 16, 2016 08:04 AM

Updated September 01, 2016 08:04 AM

Updated October 11, 2016 09:03 AM