So best practice it to use $this->t() instead of t(). However, if you use this in a service it will fail. I added this code to get t function working:
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
/**
* Class MyService.
*/
class MyService {
use StringTranslationTrait;
Add new comment