Skip to content

Instantly share code, notes, and snippets.

@ebuildy
Created January 20, 2018 12:17
Show Gist options
  • Save ebuildy/1d26f3eedf285a0da83b34be5fe971f0 to your computer and use it in GitHub Desktop.
Save ebuildy/1d26f3eedf285a0da83b34be5fe971f0 to your computer and use it in GitHub Desktop.
Make your class translate ready ! Symfony 4 translate aware trait.
namespace App\Core\Utils\Services;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\TranslatorInterface;
trait TranslatorAwareTrait
{
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* @required
*
* @param TranslatorInterface $translator
*/
public function setTranslator(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* Translates the given message.
*
* @param string $id The message id (may also be an object that can be cast to string)
* @param array $parameters An array of parameters for the message
* @param string|null $domain The domain for the message or null to use the default
* @param string|null $locale The locale or null to use the default
*
* @return string The translated string
*
* @throws InvalidArgumentException If the locale contains invalid characters
*/
public function translate($id, array $parameters = array(), $domain = null, $locale = null)
{
return $this->translator->trans($id, $parameters, $domain, $locale);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment