-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathNovaLangReorder.php
167 lines (127 loc) · 5.42 KB
/
NovaLangReorder.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Coderello\LaravelNovaLang\Commands;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use SplFileInfo;
class NovaLangReorder extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'nova-lang:reorder
{locales? : Comma-separated list of languages}
{--all : Output all languages}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Reorder the keys from Laravel Nova language files to match the source file order and output to storage folder.';
/**
* @var Filesystem
*/
protected $filesystem;
/**
* Create a new command instance.
*
* @param Filesystem $filesystem
*/
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if (!config('app.debug')) {
$this->error('This command will only run in debug mode.');
return;
}
$sourceDirectory = $this->directoryNovaSource().'/en';
$sourceFile = $sourceDirectory.'.json';
if (!$this->filesystem->exists($sourceDirectory) || !$this->filesystem->exists($sourceFile)) {
$this->error('The source language files were not found in the vendor/laravel/nova directory.');
return;
}
$outputDirectory = storage_path('app/nova-lang/reorder');
$this->filesystem->makeDirectory($outputDirectory, 0777, true, true);
$sourceKeys = array_keys(json_decode($this->filesystem->get($sourceFile), true));
$availableLocales = $this->getAvailableLocales();
$requestedLocales = $this->getRequestedLocales();
if (!$requestedLocales->count()) {
$this->error('You must either specify one or more locales, or use the --all option.');
return;
}
$requestedLocales->each(function (string $locale) use ($availableLocales, $sourceKeys, $outputDirectory) {
if (! $availableLocales->contains($locale)) {
return $this->error(sprintf('The translation file for [%s] locale does not exist. You could help other people by creating this file and sending a PR :)', $locale));
}
$inputDirectory = $this->directoryFrom().'/'.$locale;
$inputFile = $inputDirectory.'.json';
$localeKeys = json_decode($this->filesystem->get($inputFile), true);
$reorderedKeys = array_diff_assoc($sourceKeys, array_keys($localeKeys));
$outputFile = $outputDirectory.'/'.$locale.'.json';
$missingKeys = [];
if (count($reorderedKeys)) {
$outputKeys = [];
foreach ($sourceKeys as $key) {
if (isset($localeKeys[$key])) {
$outputKeys[$key] = $localeKeys[$key];
}
else {
$missingKeys[$key] = '';
}
}
$this->filesystem->put($outputFile, json_encode($outputKeys, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
$this->info(sprintf('%d translation keys for [%s] locale were out of order. The reordered file has been output to [%s].', count($reorderedKeys), $locale, $outputFile));
} elseif ($this->filesystem->exists($outputFile)) {
$this->warn(sprintf('[%s] locale has no translation keys out of order. The existing output file at [%s] was deleted.', $locale, $outputFile));
$this->filesystem->delete($outputFile);
} else {
$this->warn(sprintf('[%s] locale has no translation keys out of order. No output file was created.', $locale));
}
if (count($missingKeys)) {
$this->info(sprintf('Additionally, %d translation keys for [%s] locale were missing. Run the `nova-lang:missing` command to view them.', count($missingKeys), $locale));
}
});
}
protected function getRequestedLocales(): Collection
{
if ($this->isAll()) {
return $this->getAvailableLocales();
}
return collect(explode(',', $this->argument('locales')))->filter();
}
protected function getAvailableLocales(): Collection
{
$localesByDirectories = collect($this->filesystem->directories($this->directoryFrom()))
->map(function (string $path) {
return $this->filesystem->name($path);
});
$localesByFiles = collect($this->filesystem->files($this->directoryFrom()))
->map(function (SplFileInfo $splFileInfo) {
return str_replace('.'.$splFileInfo->getExtension(), '', $splFileInfo->getFilename());
});
return $localesByDirectories->intersect($localesByFiles)->values();
}
protected function isAll(): bool
{
return $this->option('all');
}
protected function directoryFrom(): string
{
return base_path('vendor/coderello/laravel-nova-lang/resources/lang');
}
protected function directoryNovaSource(): string
{
return base_path('vendor/laravel/nova/resources/lang');
}
}