forked from translation/laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceSaver.php
125 lines (97 loc) · 3.26 KB
/
SourceSaver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Tio\Laravel;
use Tio\Laravel\PrettyVarExport;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
class SourceSaver
{
/**
* @var Application
*/
private $application;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var PrettyVarExport
*/
private $prettyVarExport;
public function __construct(
Application $application,
FileSystem $fileSystem,
PrettyVarExport $prettyVarExport
) {
$this->application = $application;
$this->filesystem = $fileSystem;
$this->prettyVarExport = $prettyVarExport;
}
public function call($sourceEdit, $sourceLocale)
{
$key = $sourceEdit['key'];
$dir = $this->localePath($sourceLocale);
// Adapt $group and $dir if the key contains subfolders:
// https://laravel.io/forum/02-23-2015-localization-load-files-from-subdirectories-at-resourceslanglocale
if (Str::contains($key, '/')) {
$subFolders = explode('/', $key);
array_pop($subFolders);
$dir = join(DIRECTORY_SEPARATOR, array_merge([$dir], $subFolders));
}
$this->filesystem->makeDirectory($dir, 0777, true, true);
$group = $this->group($sourceEdit['key']);
$groupFile = $dir . DIRECTORY_SEPARATOR . $group . '.php';
if ($this->filesystem->exists($groupFile)) {
$translations = $this->filesystem->getRequire($groupFile);
$translations = $this->applySourceEditInTranslations($translations, $sourceEdit);
// Leave the extra newline at the end
$fileContent = <<<'EOT'
<?php
return {{translations}};
EOT;
$prettyTranslationsExport = $this->prettyVarExport->call($translations, ['array-align' => true]);
$fileContent = str_replace('{{translations}}', $prettyTranslationsExport, $fileContent);
$this->filesystem->put($groupFile, $fileContent);
}
}
private function localePath($locale)
{
return $this->path() . DIRECTORY_SEPARATOR . $locale;
}
private function path()
{
return $this->application['path.lang'];
}
private function group($key)
{
$foldersAndGroup = explode('.', $key)[0];
if (Str::contains($foldersAndGroup, '/')) {
$parts = explode('/', $foldersAndGroup);
return array_pop($parts);
}
else {
return $foldersAndGroup;
}
}
private function keys($key)
{
$keyParts = explode('.', $key);
array_shift($keyParts); // remove group part
return $keyParts;
}
private function applySourceEditInTranslations($translations, $sourceEdit)
{
$keys = $this->keys($sourceEdit['key']);
$oldText = $sourceEdit['old_text'];
$newText = $sourceEdit['new_text'];
$current = &$translations;
for ($i = 0; $i < count($keys) - 1; $i++) {
$key = $keys[$i];
$current = &$current[$key];
}
if (isset($current[$keys[count($keys) - 1]]) && $current[$keys[count($keys) - 1]] == $oldText) {
$current[$keys[count($keys) - 1]] = $newText;
}
return $translations;
}
}