Skip to content

Instantly share code, notes, and snippets.

@ozin7
Last active August 11, 2022 16:40
Show Gist options
  • Save ozin7/64931d66308c0b6b80f994f7f6200e3e to your computer and use it in GitHub Desktop.
Save ozin7/64931d66308c0b6b80f994f7f6200e3e to your computer and use it in GitHub Desktop.
Drupal 9: String translation in a Service
<?php
namespace Drupal\gurei\Service;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
/**
* Class CompanyService.
*/
class CompanyService {
use StringTranslationTrait;
/**
* CompanyService constructor.
*/
public function __construct(private TranslationInterface $stringTranslation) {}
public function getMessage() {
return $this->t('Welcome home');
}
}
services:
gurei.company_manager:
class: Drupal\gurei\Service\CompanyService
arguments:
- '@string_translation'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment