I’m trying to use translation in a custom twig filter like this
public function formatTime($timestamp)
{
$str = date('j %\m%',$timestamp);
$str = str_replace($str, '%m%', $this->get('translator')->trans('month'.date('m', $timestamp) ) );
return $str;
}
offcourse get is unknown function.
Should I make my Extension aware of the environment or simply request translation class to make it available ?
You can inject a
translatorservice into your class:And then store an instance of translator in the protected field and use it later:
UPD1: configuration for YAML service definition: