<?php
namespace App\Twig;
use App\Service\TranslationService;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
/**
* Class AppExtension
* @package App\Twig
*/
class AppExtension extends AbstractExtension
{
/**
* @var TranslationService
*/
private $translationService;
/**
* @var SessionInterface
*/
private $session;
/**
* AppExtension constructor.
* @param TranslationService $translationService
* @param SessionInterface $session
*/
public function __construct(TranslationService $translationService, SessionInterface $session)
{
$this->translationService = $translationService;
$this->session = $session;
}
/**
* @return TwigFilter[]
*/
public function getFilters()
{
return [
new TwigFilter('transedit', [$this, 'getTranslation']),
new TwigFilter('transonly', [$this, 'getTranslationOnly']),
new TwigFilter('json_decode', [$this, 'jsonDecode'])
];
}
/**
* @param string $name
* @return string
*/
public function getTranslation(string $name)
{
// load language from user
$translation = $this->translationService->getTranslation($name);
if ($this->session->get("editmode", false)) {
return "<span class=\"transedit\" data-name=\"".$name."\" data-translation=\"".$translation."\">".$translation."</span>";
} else {
return $translation;
}
}
/**
* @param string $name
* @return string
*/
public function getTranslationOnly(string $name)
{
// load language from user
return $this->translationService->getTranslation($name);
}
/**
* @param string $name
* @return string
*/
public function jsonDecode($string)
{
return json_decode($string, true);
}
}