• Reset your password
Home
OO PHP, Drupal, Symfony Developer and Architect
Tim Rabbetts

Main navigation

  • Home
  • Blog
  • Log in

Drupal 8: Adding t function to a service

Breadcrumb

  • Home
  • blog
  • drupal 8 adding t function to a service
  • Drupal 8: Adding t function to a service
Tim
Rabbetts
By zarexogre | Thu, 02/04/2020
computer, pc, workplace
drupal

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;