You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TranslatorTwigExtension.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Lc\SovBundle\Twig;
  3. use App\Repository\ReminderRepository;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\SovBundle\Model\Reminder\ReminderInterface;
  6. use Lc\SovBundle\Repository\Reminder\ReminderStore;
  7. use Lc\SovBundle\Translation\TranslatorAdmin;
  8. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpKernel\KernelInterface;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use Twig\Extension\AbstractExtension;
  16. use Twig\TwigFilter;
  17. use Twig\TwigFunction;
  18. class TranslatorTwigExtension extends AbstractExtension
  19. {
  20. protected $parameterBag;
  21. protected $requestStack;
  22. protected $router;
  23. protected $translatorAdmin;
  24. public function __construct(
  25. ParameterBagInterface $parameterBag,
  26. RequestStack $requestStack,
  27. UrlGeneratorInterface $router,
  28. TranslatorAdmin $translatorAdmin
  29. ) {
  30. $this->parameterBag = $parameterBag;
  31. $this->requestStack = $requestStack;
  32. $this->router = $router;
  33. $this->translatorAdmin = $translatorAdmin;
  34. }
  35. public function getFunctions()
  36. {
  37. return [
  38. new TwigFunction('sov_translated_urls', [$this, 'getTranslatedUrls']),
  39. ];
  40. }
  41. public function getFilters()
  42. {
  43. return [
  44. new TwigFilter('sov_trans_admin_field', [$this, 'transAdminField']),
  45. new TwigFilter('sov_trans_admin_help', [$this, 'transAdminHelp']),
  46. new TwigFilter('sov_trans_admin_panel', [$this, 'transAdminPanel']),
  47. new TwigFilter('sov_trans_admin_menu', [$this, 'transAdminMenu']),
  48. new TwigFilter('sov_trans_admin_title', [$this, 'transAdminTitle']),
  49. new TwigFilter('sov_trans_admin_action', [$this, 'transAdminAction']),
  50. ];
  51. }
  52. function getTranslatedUrls()
  53. {
  54. $ret = array();
  55. $langs = $this->parameterBag->get('app.locales');
  56. $currentRoute = $this->requestStack->getCurrentRequest()->get('_route');
  57. $params = array_merge((array)$this->requestStack->getCurrentRequest()->get('_route_params'), $_GET);
  58. if ($currentRoute) {
  59. foreach ($langs as $lg) {
  60. $ret[$lg] = $this->router->generate($currentRoute, array_merge($params, array('_locale' => $lg)));
  61. }
  62. }
  63. return $ret;
  64. }
  65. public function transAdminField($fieldName, $entityClass)
  66. {
  67. return $this->translatorAdmin->transField($fieldName, $entityClass);
  68. }
  69. public function transAdminHelp($fieldName, $entityClass)
  70. {
  71. return $this->translatorAdmin->transHelp($fieldName, $entityClass);
  72. }
  73. public function transAdminPanel($panelName, $entityClass)
  74. {
  75. return $this->translatorAdmin->transPanel($panelName, $entityClass);
  76. }
  77. public function transAdminMenu($menuName)
  78. {
  79. return $this->translatorAdmin->transMenu($menuName);;
  80. }
  81. public function transAdminAction($actionName)
  82. {
  83. return $this->translatorAdmin->transAction($actionName);;
  84. }
  85. public function transAdminTitle($pageName, $entityClass = null, $params = [])
  86. {
  87. return $this->translatorAdmin->transTitle($pageName, $entityClass, $params);
  88. }
  89. }