1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ben182\AutoTranslate; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Themsaid\Langman\Manager as Langman; |
7
|
|
|
use Ben182\AutoTranslate\Translators\TranslatorInterface; |
8
|
|
|
|
9
|
|
|
class AutoTranslate |
10
|
|
|
{ |
11
|
|
|
protected $manager; |
12
|
|
|
public $translator; |
13
|
|
|
|
14
|
24 |
|
public function __construct(Langman $manager, TranslatorInterface $translator) |
15
|
|
|
{ |
16
|
24 |
|
$this->manager = $manager; |
17
|
24 |
|
$this->translator = $translator; |
18
|
24 |
|
$this->translator->setSource(config('auto-translate.source_language')); |
19
|
24 |
|
} |
20
|
|
|
|
21
|
12 |
|
public function getSourceTranslations() |
22
|
|
|
{ |
23
|
12 |
|
return $this->getTranslations(config('auto-translate.source_language')); |
24
|
|
|
} |
25
|
|
|
|
26
|
15 |
|
public function getTranslations(string $lang) |
27
|
|
|
{ |
28
|
15 |
|
$aReturn = []; |
29
|
|
|
|
30
|
15 |
|
$files = $this->manager->files(); |
31
|
|
|
|
32
|
15 |
|
foreach ($files as $fileKeyName => $languagesFile) { |
33
|
15 |
|
if (! isset($languagesFile[$lang])) { |
34
|
3 |
|
continue; |
35
|
|
|
} |
36
|
|
|
|
37
|
15 |
|
$allTranslations = $this->manager->getFileContent($languagesFile[$lang]); |
38
|
|
|
|
39
|
15 |
|
$aReturn[$fileKeyName] = $allTranslations; |
40
|
|
|
} |
41
|
|
|
|
42
|
15 |
|
return $aReturn; |
43
|
|
|
} |
44
|
|
|
|
45
|
9 |
|
public function getMissingTranslations(string $lang) |
46
|
|
|
{ |
47
|
9 |
|
$source = $this->getSourceTranslations(); |
48
|
9 |
|
$lang = $this->getTranslations($lang); |
49
|
|
|
|
50
|
9 |
|
$dottedSource = Arr::dot($source); |
51
|
9 |
|
$dottedlang = Arr::dot($lang); |
52
|
|
|
|
53
|
9 |
|
$diff = array_diff(array_keys($dottedSource), array_keys($dottedlang)); |
54
|
|
|
|
55
|
9 |
|
return collect($dottedSource)->only($diff); |
56
|
|
|
} |
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) |
111
|
|
|
{ |
112
|
3 |
|
foreach ($data as $languageFileKey => $translations) { |
113
|
|
|
$translations = array_map(function ($item) use ($language) { |
114
|
|
|
return [ |
115
|
3 |
|
$language => $item, |
116
|
|
|
]; |
117
|
3 |
|
}, $translations); |
118
|
|
|
|
119
|
3 |
|
$this->manager->fillKeys($languageFileKey, $translations); |
120
|
|
|
} |
121
|
3 |
|
} |
122
|
|
|
|
123
|
9 |
|
public function array_undot(array $dottedArray, array $initialArray = []) : array |
124
|
|
|
{ |
125
|
9 |
|
foreach ($dottedArray as $key => $value) { |
126
|
9 |
|
Arr::set($initialArray, $key, $value); |
127
|
|
|
} |
128
|
|
|
|
129
|
9 |
|
return $initialArray; |
130
|
|
|
} |
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.