1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\TranslationBundle\Twig\Extension; |
15
|
|
|
|
16
|
|
|
use Sonata\TranslationBundle\Checker\TranslatableChecker; |
17
|
|
|
use Symfony\Component\Intl\Intl; |
18
|
|
|
use Twig\Extension\AbstractExtension; |
19
|
|
|
use Twig\TwigFilter; |
20
|
|
|
use Twig\TwigTest; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Nicolas Bastien <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class SonataTranslationExtension extends AbstractExtension |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var TranslatableChecker |
29
|
|
|
*/ |
30
|
|
|
protected $translatableChecker; |
31
|
|
|
|
32
|
|
|
public function __construct(TranslatableChecker $translatableChecker) |
33
|
|
|
{ |
34
|
|
|
$this->translatableChecker = $translatableChecker; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getName(): string |
38
|
|
|
{ |
39
|
|
|
return 'sonata_translation'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function setTranslatableChecker(TranslatableChecker $translatableChecker): void |
43
|
|
|
{ |
44
|
|
|
$this->translatableChecker = $translatableChecker; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getTranslatableChecker(): TranslatableChecker |
48
|
|
|
{ |
49
|
|
|
return $this->translatableChecker; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getTests() |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
|
|
new TwigTest('translatable', [$this, 'isTranslatable']), |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getFilters() |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
new TwigFilter('localeName', [$this, 'getLocaleName']), |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Check if $object is translatable. |
68
|
|
|
* |
69
|
|
|
* @param mixed $object |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function isTranslatable($object): bool |
74
|
|
|
{ |
75
|
|
|
return $this->getTranslatableChecker()->isTranslatable($object); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getLocaleName(string $locale, ?string $displayLocale = null): ?string |
79
|
|
|
{ |
80
|
|
|
return Intl::getLocaleBundle()->getLocaleName($locale, $displayLocale); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|