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.

131 lines
4.3KB

  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_field_index', [$this, 'transAdminFieldIndex']),
  39. new TwigFilter('sov_trans_admin_help', [$this, 'transAdminHelp']),
  40. new TwigFilter('sov_trans_admin_panel', [$this, 'transAdminPanel']),
  41. new TwigFilter('sov_trans_admin_modal', [$this, 'transAdminModal']),
  42. new TwigFilter('sov_trans_admin_card', [$this, 'transAdminCard']),
  43. new TwigFilter('sov_trans_admin_box', [$this, 'transAdminBox']),
  44. new TwigFilter('sov_trans_admin_menu', [$this, 'transAdminMenu']),
  45. new TwigFilter('sov_trans_admin_title', [$this, 'transAdminTitle']),
  46. new TwigFilter('sov_trans_admin_action', [$this, 'transAdminAction']),
  47. new TwigFilter('sov_trans_admin_choice', [$this, 'transAdminChoice']),
  48. ];
  49. }
  50. function getTranslatedUrls()
  51. {
  52. $ret = array();
  53. $langs = $this->parameterBag->get('app.locales');
  54. $currentRoute = $this->requestStack->getCurrentRequest()->get('_route');
  55. $params = array_merge((array)$this->requestStack->getCurrentRequest()->get('_route_params'), $_GET);
  56. if ($currentRoute) {
  57. foreach ($langs as $lg) {
  58. $ret[$lg] = $this->router->generate($currentRoute, array_merge($params, array('_locale' => $lg)));
  59. }
  60. }
  61. return $ret;
  62. }
  63. public function transAdminField($fieldName, $entityClass)
  64. {
  65. return $this->translatorAdmin->transField($fieldName, $entityClass);
  66. }
  67. public function transAdminFieldIndex($fieldName, $entityClass)
  68. {
  69. return $this->translatorAdmin->transFieldIndex($fieldName, $entityClass);
  70. }
  71. public function transAdminChoice($choice, $fieldName, $entityClass)
  72. {
  73. return $this->translatorAdmin->transChoice($entityClass, $fieldName, $choice);
  74. }
  75. public function transAdminHelp($fieldName, $entityClass)
  76. {
  77. return $this->translatorAdmin->transHelp($fieldName, $entityClass);
  78. }
  79. public function transAdminPanel($panelName, $entityClass)
  80. {
  81. return $this->translatorAdmin->transPanel($panelName, $entityClass);
  82. }
  83. public function transAdminModal($modalName, $entityClass)
  84. {
  85. return $this->translatorAdmin->transModal($modalName, $entityClass);
  86. }
  87. public function transAdminBox($boxName, $entityClass= 'default')
  88. {
  89. return $this->translatorAdmin->transBox($boxName, $entityClass);
  90. }
  91. public function transAdminCard($cardName, $entityClass)
  92. {
  93. return $this->translatorAdmin->transCard($cardName, $entityClass);
  94. }
  95. public function transAdminMenu($menuName)
  96. {
  97. return $this->translatorAdmin->transMenu($menuName);;
  98. }
  99. public function transAdminAction($actionName)
  100. {
  101. return $this->translatorAdmin->transAction($actionName);;
  102. }
  103. public function transAdminTitle($pageName, $entityClass = null, $params = [])
  104. {
  105. return $this->translatorAdmin->transTitle($pageName, $entityClass, $params);
  106. }
  107. }