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.
\Drupal
object. It's an opinion. I agree, but I don't agree enough to go out of my way to not use StringTranslationTrait.