1 | <?php |
||
9 | class AutoTranslate |
||
10 | { |
||
11 | protected $manager; |
||
12 | public $translator; |
||
13 | |||
14 | 24 | public function __construct(Langman $manager, TranslatorInterface $translator) |
|
20 | |||
21 | 12 | public function getSourceTranslations() |
|
25 | |||
26 | 15 | public function getTranslations(string $lang) |
|
44 | |||
45 | 9 | public function getMissingTranslations(string $lang) |
|
57 | |||
58 | 6 | public function translate(string $targetLanguage, $data, $callbackAfterEachTranslation = null) |
|
59 | { |
||
60 | 6 | $this->translator->setTarget($targetLanguage); |
|
61 | |||
62 | 6 | $dottedSource = Arr::dot($data); |
|
63 | |||
64 | 6 | foreach ($dottedSource as $key => $value) { |
|
65 | 6 | if ($value === '') { |
|
66 | 3 | $dottedSource[$key] = $value; |
|
67 | |||
68 | 3 | if ($callbackAfterEachTranslation) { |
|
69 | $callbackAfterEachTranslation(); |
||
70 | } |
||
71 | 3 | continue; |
|
72 | } |
||
73 | |||
74 | 3 | $variables = $this->findVariables($value); |
|
75 | |||
76 | 3 | $dottedSource[$key] = is_string($value) ? $this->translator->translate($value) : $value; |
|
77 | |||
78 | 3 | $dottedSource[$key] = $this->replaceTranslatedVariablesWithOld($variables, $dottedSource[$key]); |
|
79 | |||
80 | 3 | if ($callbackAfterEachTranslation) { |
|
81 | $callbackAfterEachTranslation(); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | 6 | return $this->array_undot($dottedSource); |
|
86 | } |
||
87 | |||
88 | 3 | public function findVariables($string) |
|
89 | { |
||
90 | 3 | $m = null; |
|
91 | |||
92 | 3 | if (is_string($string)) { |
|
93 | 3 | preg_match_all('/:\S+/', $string, $m); |
|
94 | } |
||
95 | |||
96 | 3 | return $m; |
|
97 | } |
||
98 | |||
99 | 3 | public function replaceTranslatedVariablesWithOld($variables, $string) |
|
100 | { |
||
101 | 3 | if (isset($variables[0])) { |
|
102 | 3 | $replacements = $variables[0]; |
|
103 | |||
104 | return preg_replace_callback('/:\S+/', function ($matches) use (&$replacements) { |
||
|
|||
105 | return array_shift($replacements); |
||
106 | 3 | }, $string); |
|
107 | } |
||
108 | } |
||
109 | |||
110 | 3 | public function fillLanguageFiles(string $language, array $data) |
|
122 | |||
123 | 9 | public function array_undot(array $dottedArray, array $initialArray = []) : array |
|
124 | { |
||
125 | 9 | foreach ($dottedArray as $key => $value) { |
|
131 | } |
||
132 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.