1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @author Xavier Chopin <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace App\TwigExtension; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
13
|
|
|
use Twig_Extension; |
14
|
|
|
use Twig_SimpleFunction; |
15
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class TranslatorExtension |
19
|
|
|
* Useful Twig function for translating words or getting the languages available. |
20
|
|
|
* |
21
|
|
|
* @package AppBundle\TwigExtension |
22
|
|
|
*/ |
23
|
|
|
class TranslatorExtension extends Twig_Extension |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $country_id; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ContainerInterface |
33
|
|
|
*/ |
34
|
|
|
protected $container; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* TranslatorExtension constructor. |
38
|
|
|
* |
39
|
|
|
* @param ContainerInterface $container |
40
|
|
|
* @param RequestStack $request_stack |
41
|
|
|
*/ |
42
|
|
|
public function __construct(ContainerInterface $container, RequestStack $request_stack) |
43
|
|
|
{ |
44
|
|
|
$this->container = $container; |
45
|
|
|
$request = $request_stack->getCurrentRequest(); |
46
|
|
|
$this->country_id = $request->get('_locale'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function getName() |
53
|
|
|
{ |
54
|
|
|
return 'translate'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function getFunctions() |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
new Twig_SimpleFunction('dictionary', [$this, 'dictionary']), |
64
|
|
|
new Twig_SimpleFunction('languages', [$this, 'languagesAvailable']), |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns a dictionary of keywords for the current language. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function dictionary() |
74
|
|
|
{ |
75
|
|
|
$dictionary = $this->container->getParameter('dictionaries')[$this->country_id]; |
76
|
|
|
return $dictionary; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the list of the languages translated. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function languagesAvailable() |
85
|
|
|
{ |
86
|
|
|
$languages = []; |
87
|
|
|
foreach ($this->container->getParameter('dictionaries') as $languageId => $dictionary) { |
88
|
|
|
$languages += [ $dictionary['self_name'] => $languageId ]; |
89
|
|
|
} |
90
|
|
|
return $languages; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|