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
|
21 |
|
public function __construct(Langman $manager, TranslatorInterface $translator) |
15
|
|
|
{ |
16
|
21 |
|
$this->manager = $manager; |
17
|
21 |
|
$this->translator = $translator; |
18
|
21 |
|
$this->translator->setSource(config('auto-translate.source_language')); |
19
|
21 |
|
} |
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
|
3 |
|
public function translate(string $targetLanguage, $data, $callbackAfterEachTranslation = null) |
59
|
|
|
{ |
60
|
3 |
|
$this->translator->setTarget($targetLanguage); |
61
|
|
|
|
62
|
3 |
|
$dottedSource = Arr::dot($data); |
63
|
|
|
|
64
|
3 |
|
foreach ($dottedSource as $key => $value) { |
65
|
3 |
|
$variables = $this->findVariables($value); |
66
|
|
|
|
67
|
3 |
|
$dottedSource[$key] = is_string($value) ? $this->translator->translate($value) : $value; |
68
|
|
|
|
69
|
3 |
|
$dottedSource[$key] = $this->replaceTranslatedVariablesWithOld($variables, $dottedSource[$key]); |
70
|
|
|
|
71
|
3 |
|
if ($callbackAfterEachTranslation) { |
72
|
|
|
$callbackAfterEachTranslation(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
return $this->array_undot($dottedSource); |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
public function findVariables($string) |
80
|
|
|
{ |
81
|
3 |
|
$m = null; |
82
|
|
|
|
83
|
3 |
|
if (is_string($string)) { |
84
|
3 |
|
preg_match_all('/:\S+/', $string, $m); |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
return $m; |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
public function replaceTranslatedVariablesWithOld($variables, $string) |
91
|
|
|
{ |
92
|
3 |
|
if (isset($variables[0])) { |
93
|
3 |
|
$replacements = $variables[0]; |
94
|
|
|
|
95
|
|
|
return preg_replace_callback('/:\S+/', function ($matches) use (&$replacements) { |
|
|
|
|
96
|
|
|
return array_shift($replacements); |
97
|
3 |
|
}, $string); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
public function fillLanguageFiles(string $language, array $data) |
102
|
|
|
{ |
103
|
3 |
|
foreach ($data as $languageFileKey => $translations) { |
104
|
|
|
$translations = array_map(function ($item) use ($language) { |
105
|
|
|
return [ |
106
|
3 |
|
$language => $item, |
107
|
|
|
]; |
108
|
3 |
|
}, $translations); |
109
|
|
|
|
110
|
3 |
|
$this->manager->fillKeys($languageFileKey, $translations); |
111
|
|
|
} |
112
|
3 |
|
} |
113
|
|
|
|
114
|
6 |
|
public function array_undot(array $dottedArray, array $initialArray = []) : array |
115
|
|
|
{ |
116
|
6 |
|
foreach ($dottedArray as $key => $value) { |
117
|
6 |
|
Arr::set($initialArray, $key, $value); |
118
|
|
|
} |
119
|
|
|
|
120
|
6 |
|
return $initialArray; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.