|
1
|
|
|
<?php namespace Modules\Translation\Services; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
4
|
|
|
use Modules\Translation\ValueObjects\TranslationGroup; |
|
5
|
|
|
|
|
6
|
|
|
class TranslationsWriter |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @var Filesystem |
|
10
|
|
|
*/ |
|
11
|
|
|
private $finder; |
|
12
|
|
|
/** |
|
13
|
|
|
* @var TranslationsService |
|
14
|
|
|
*/ |
|
15
|
|
|
private $translationsService; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(TranslationsService $translationsService, Filesystem $finder) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->finder = $finder; |
|
20
|
|
|
$this->translationsService = $translationsService; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Export all translations back to disk |
|
25
|
|
|
*/ |
|
26
|
|
|
public function export() |
|
27
|
|
|
{ |
|
28
|
|
|
$translations = $this->translationsService->getFileAndDatabaseMergedTranslations(); |
|
29
|
|
|
|
|
30
|
|
|
$tree = $this->makeTree($translations); |
|
31
|
|
|
|
|
32
|
|
|
foreach ($tree as $locale => $groups) { |
|
33
|
|
|
foreach ($groups as $moduleName => $fileGroup) { |
|
34
|
|
|
foreach ($fileGroup as $file => $data) { |
|
35
|
|
|
$path = $this->getTranslationsDirectory() . $moduleName . '/' . $locale . '/' . $file . '.php'; |
|
36
|
|
|
$output = "<?php\n\nreturn " . var_export($data, true) . ";\n"; |
|
37
|
|
|
$this->finder->put($path, $output); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the module name from the given key |
|
45
|
|
|
* @param string $key |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
private function getModuleNameFrom($key) |
|
49
|
|
|
{ |
|
50
|
|
|
return substr($key, 0, strpos($key, '::')); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
private function getTranslationsDirectory() |
|
57
|
|
|
{ |
|
58
|
|
|
return __DIR__ . '/../Resources/lang/'; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get the file name from the given key |
|
63
|
|
|
* @param string $key |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
private function getFileNameFrom($key) |
|
67
|
|
|
{ |
|
68
|
|
|
$key = str_replace($this->getModuleNameFrom($key) . '::', '', $key); |
|
69
|
|
|
|
|
70
|
|
|
return substr($key, 0, strpos($key, '.')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Make a usable array |
|
75
|
|
|
* @param TranslationGroup $translations |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
private function makeTree(TranslationGroup $translations) |
|
79
|
|
|
{ |
|
80
|
|
|
$tree = []; |
|
81
|
|
|
|
|
82
|
|
|
foreach ($translations->allRaw() as $locale => $translation) { |
|
83
|
|
|
foreach ($translation as $key => $trans) { |
|
84
|
|
|
$moduleName = $this->getModuleNameFrom($key); |
|
85
|
|
|
$fileName = $this->getFileNameFrom($key); |
|
86
|
|
|
$key = str_replace($moduleName . '::' . $fileName . '.', '', $key); |
|
87
|
|
|
|
|
88
|
|
|
array_set($tree[$locale][$moduleName][$fileName], $key, $trans); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $tree; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|