Skip to main content

Drupal 8: Adding t function to a service

Updated by Tim Rabbetts on
computer, pc, workplace

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