|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LAG\AdminBundle\Translation\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use LAG\AdminBundle\Admin\Helper\AdminHelperInterface; |
|
8
|
|
|
use LAG\AdminBundle\Configuration\ApplicationConfiguration; |
|
9
|
|
|
use Symfony\Component\String\UnicodeString; |
|
10
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
11
|
|
|
|
|
12
|
|
|
class TranslationHelper implements TranslationHelperInterface |
|
13
|
|
|
{ |
|
14
|
|
|
private TranslatorInterface $translator; |
|
15
|
|
|
private ApplicationConfiguration $appConfig; |
|
16
|
|
|
private AdminHelperInterface $adminHelper; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct( |
|
19
|
|
|
TranslatorInterface $translator, |
|
20
|
|
|
ApplicationConfiguration $appConfig, |
|
21
|
|
|
AdminHelperInterface $adminHelper |
|
22
|
|
|
) { |
|
23
|
|
|
$this->translator = $translator; |
|
24
|
|
|
$this->appConfig = $appConfig; |
|
25
|
|
|
$this->adminHelper = $adminHelper; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Return the translation pattern with keys "{admin}" and "{key}" replaced by their values. |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public static function getTranslationKey( |
|
32
|
|
|
string $translationPattern, |
|
33
|
|
|
string $adminName, |
|
34
|
|
|
string $key |
|
35
|
|
|
): string { |
|
36
|
1 |
|
$u = new UnicodeString($key); |
|
37
|
1 |
|
$u = $u->snake(); |
|
38
|
1 |
|
$translationPattern = str_replace('{key}', $u->toString(), $translationPattern); |
|
39
|
|
|
|
|
40
|
1 |
|
return str_replace('{admin}', $adminName, $translationPattern); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->translator->trans($id, $parameters, $domain, $locale); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function transWithPattern( |
|
49
|
|
|
string $id, |
|
50
|
|
|
array $parameters = [], |
|
51
|
|
|
string $domain = null, |
|
52
|
|
|
string $locale = null, |
|
53
|
|
|
string $pattern = null, |
|
54
|
|
|
string $adminName = null |
|
55
|
|
|
): string { |
|
56
|
|
|
if ($pattern === null) { |
|
57
|
|
|
$pattern = $this->appConfig->getTranslationPattern(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($domain === null) { |
|
61
|
|
|
$domain = $this->appConfig->getTranslationCatalog(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($adminName === null) { |
|
65
|
|
|
$adminName = $this->adminHelper->getAdmin()->getName(); |
|
66
|
|
|
} |
|
67
|
|
|
$id = self::getTranslationKey( |
|
68
|
|
|
$pattern, |
|
69
|
|
|
$adminName, |
|
70
|
|
|
$id |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
return $this->trans($id, [], $domain, $locale); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|