|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Configuration\Behavior; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Utils\TranslationUtils; |
|
6
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
|
7
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
9
|
|
|
|
|
10
|
|
|
trait TranslationConfigurationTrait |
|
11
|
|
|
{ |
|
12
|
|
|
public abstract function get($name); |
|
13
|
|
|
|
|
14
|
|
|
protected function configureTranslation(OptionsResolver $resolver, string $pattern = 'lag.{admin}.{key}', string $catalog = 'messages') |
|
15
|
|
|
{ |
|
16
|
|
|
$resolver |
|
17
|
|
|
->setDefaults([ |
|
18
|
|
|
'translation' => function (OptionsResolver $subResolver) use ($pattern, $catalog) { |
|
19
|
|
|
$subResolver |
|
20
|
|
|
->setDefaults([ |
|
21
|
|
|
'enabled' => true, |
|
22
|
|
|
'pattern' => $pattern, |
|
23
|
|
|
'catalog' => $catalog, |
|
24
|
|
|
]) |
|
25
|
|
|
; |
|
26
|
|
|
}, |
|
27
|
|
|
]) |
|
28
|
|
|
->setAllowedTypes('translation', 'array') |
|
29
|
|
|
->setNormalizer('translation', function (Options $options, $value) { |
|
30
|
|
|
if (!array_key_exists('enabled', $value)) { |
|
31
|
|
|
throw new InvalidOptionsException('Admin translation enabled parameter should be defined'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if (!is_bool($value['enabled'])) { |
|
35
|
|
|
throw new InvalidOptionsException('Admin translation enabled parameter should be a boolean'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (!array_key_exists('pattern', $value)) { |
|
39
|
|
|
$value['pattern'] = '{admin}.{key}'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ($value['enabled'] && false === strstr($value['pattern'], '{key}')) { |
|
43
|
|
|
throw new InvalidOptionsException('Admin translation pattern should contains the {key} placeholder, given "'.$value['pattern'].'"'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $value; |
|
47
|
|
|
}) |
|
48
|
|
|
; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function isTranslationEnabled(): bool |
|
52
|
|
|
{ |
|
53
|
|
|
return true === $this->get('translation')['enabled']; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getTranslationPattern(): string |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->get('translation')['pattern']; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getTranslationCatalog(): string |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->get('translation')['catalog']; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getTranslationKey(string $text, $adminName = null): string |
|
67
|
|
|
{ |
|
68
|
|
|
$text = ucfirst($text); |
|
69
|
|
|
|
|
70
|
|
|
if (!$this->isTranslationEnabled()) { |
|
71
|
|
|
$text = TranslationUtils::getTranslationKey( |
|
72
|
|
|
$this->getTranslationPattern(), |
|
73
|
|
|
$adminName, |
|
74
|
|
|
'create' |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $text; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|