Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

TranslatorTwigExtension.php 3.1KB

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