In your custom_module/src/myCustomService.php class file, modify your custom service and add Dependency Injection of Drupal\Core\StringTranslation\TranslationInterface like the example below:
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
class MyClass {
use StringTranslationTrait;
/**
* Constructs a MyClass object.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service.
*/
public function __construct(TranslationInterface $string_translation) {
// You can skip injecting this service, the trait will fallback to \Drupal::translation()
// but it is recommended to do so, for easier testability,
$this->stringTranslation = $string_translation;
}
/**
* Does something.
*/
public function doSth() {
// ...
$string = $this->t('Something');
// ...
}
}
You custom.services.yml should look like this
services:
api_charts.data:
class: Drupal\custom_module\CustomDataService
arguments:
- '@string_translation'
arguments:
- '@string_translation' is a mandatory argument.