|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Admin Bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2015-2016 LIN3S <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LIN3S\CMSKernel\Infrastructure\Lin3sAdminBundle\Twig; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Twig action translation filter. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Beñat Espiña <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class TwigActionTranslationFilter extends \Twig_Extension |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The translator. |
|
25
|
|
|
* |
|
26
|
|
|
* @var TranslatorInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $translator; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param TranslatorInterface $translator The translator |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(TranslatorInterface $translator) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->translator = $translator; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getFilters() |
|
44
|
|
|
{ |
|
45
|
|
|
return [ |
|
46
|
|
|
'action_translation' => new \Twig_Filter_Method($this, 'actionTranslation'), |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getName() |
|
54
|
|
|
{ |
|
55
|
|
|
return 'cms_kernel_admin_bridge_twig_action_translation'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Callback of action translation Twig filter that returns resultant array. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $options The action options |
|
62
|
|
|
* |
|
63
|
|
|
* @return array|string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function actionTranslation($options) |
|
66
|
|
|
{ |
|
67
|
|
|
if (!is_array($options)) { |
|
68
|
|
|
return $this->translator->trans($options, [], 'CmsKernelAdminBridge'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$resultOptions = []; |
|
72
|
|
|
foreach ($options as $optionKey => $option) { |
|
73
|
|
|
$option = null === json_decode($option, true) ? $option : json_decode($option, true); |
|
74
|
|
|
|
|
75
|
|
|
if (is_array($option)) { |
|
76
|
|
|
foreach ($option as $iterationKey => $iteration) { |
|
77
|
|
|
if (is_array($iteration)) { |
|
78
|
|
|
foreach ($iteration as $iKey => $i) { |
|
79
|
|
|
if (is_array($i)) { |
|
80
|
|
|
foreach ($i as $iiKey => $ii) { |
|
81
|
|
|
$resultOptions[$optionKey][$iterationKey][$iKey][$iiKey] = $this->translator->trans($ii); |
|
82
|
|
|
} |
|
83
|
|
|
} else { |
|
84
|
|
|
$resultOptions[$optionKey][$iterationKey][$iKey] = $this->translator->trans($i); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} else { |
|
88
|
|
|
$resultOptions[$optionKey][$iterationKey] = $this->translator->trans($iteration); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} else { |
|
92
|
|
|
$resultOptions[$optionKey] = $this->translator->trans($option); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $resultOptions; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|