Skip to content

Commit e71f64c

Browse files
authored
Command to reset the order of translated keys (#190)
1 parent ce887db commit e71f64c

File tree

3 files changed

+173
-13
lines changed

3 files changed

+173
-13
lines changed

src/Commands/NovaLangMissing.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,6 @@ public function handle()
6969

7070
$sourceKeys = array_keys(json_decode($this->filesystem->get($sourceFile), true));
7171

72-
if (!in_array(':resource Details', $sourceKeys)) { // Temporary fix until laravel/nova#463 is merged
73-
$sourceKeys = array_unique(array_merge($sourceKeys, [
74-
'Action',
75-
'Changes',
76-
'Original',
77-
'This resource no longer exists',
78-
':resource Details',
79-
]));
80-
}
81-
8272
$availableLocales = $this->getAvailableLocales();
8373

8474
$requestedLocales = $this->getRequestedLocales();

src/Commands/NovaLangReorder.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
namespace Coderello\LaravelNovaLang\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
use Illuminate\Support\Collection;
8+
use SplFileInfo;
9+
10+
class NovaLangReorder extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'nova-lang:reorder
18+
{locales? : Comma-separated list of languages}
19+
{--all : Output all languages}';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Reorder the keys from Laravel Nova language files to match the source file order and output to storage folder.';
27+
28+
/**
29+
* @var Filesystem
30+
*/
31+
protected $filesystem;
32+
33+
/**
34+
* Create a new command instance.
35+
*
36+
* @param Filesystem $filesystem
37+
*/
38+
public function __construct(Filesystem $filesystem)
39+
{
40+
$this->filesystem = $filesystem;
41+
42+
parent::__construct();
43+
}
44+
45+
/**
46+
* Execute the console command.
47+
*
48+
* @return mixed
49+
*/
50+
public function handle()
51+
{
52+
if (!config('app.debug')) {
53+
$this->error('This command will only run in debug mode.');
54+
55+
return;
56+
}
57+
58+
$sourceDirectory = $this->directoryNovaSource().'/en';
59+
$sourceFile = $sourceDirectory.'.json';
60+
61+
if (!$this->filesystem->exists($sourceDirectory) || !$this->filesystem->exists($sourceFile)) {
62+
$this->error('The source language files were not found in the vendor/laravel/nova directory.');
63+
64+
return;
65+
}
66+
67+
$outputDirectory = storage_path('app/nova-lang/reorder');
68+
$this->filesystem->makeDirectory($outputDirectory, 0777, true, true);
69+
70+
$sourceKeys = array_keys(json_decode($this->filesystem->get($sourceFile), true));
71+
72+
$availableLocales = $this->getAvailableLocales();
73+
74+
$requestedLocales = $this->getRequestedLocales();
75+
76+
if (!$requestedLocales->count()) {
77+
$this->error('You must either specify one or more locales, or use the --all option.');
78+
return;
79+
}
80+
81+
$requestedLocales->each(function (string $locale) use ($availableLocales, $sourceKeys, $outputDirectory) {
82+
83+
if (! $availableLocales->contains($locale)) {
84+
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));
85+
}
86+
87+
$inputDirectory = $this->directoryFrom().'/'.$locale;
88+
89+
$inputFile = $inputDirectory.'.json';
90+
91+
$localeKeys = json_decode($this->filesystem->get($inputFile), true);
92+
93+
$reorderedKeys = array_diff_assoc($sourceKeys, array_keys($localeKeys));
94+
95+
$outputFile = $outputDirectory.'/'.$locale.'.json';
96+
97+
$missingKeys = [];
98+
99+
if (count($reorderedKeys)) {
100+
101+
$outputKeys = [];
102+
foreach ($sourceKeys as $key) {
103+
if (isset($localeKeys[$key])) {
104+
$outputKeys[$key] = $localeKeys[$key];
105+
}
106+
else {
107+
$missingKeys[$key] = '';
108+
}
109+
}
110+
111+
$this->filesystem->put($outputFile, json_encode($outputKeys, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
112+
113+
$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));
114+
115+
} elseif ($this->filesystem->exists($outputFile)) {
116+
$this->warn(sprintf('[%s] locale has no translation keys out of order. The existing output file at [%s] was deleted.', $locale, $outputFile));
117+
$this->filesystem->delete($outputFile);
118+
} else {
119+
$this->warn(sprintf('[%s] locale has no translation keys out of order. No output file was created.', $locale));
120+
}
121+
122+
if (count($missingKeys)) {
123+
$this->info(sprintf('Additionally, %d translation keys for [%s] locale were missing. Run the `nova-lang:missing` command to view them.', count($missingKeys), $locale));
124+
}
125+
126+
});
127+
}
128+
129+
protected function getRequestedLocales(): Collection
130+
{
131+
if ($this->isAll()) {
132+
return $this->getAvailableLocales();
133+
}
134+
135+
return collect(explode(',', $this->argument('locales')))->filter();
136+
}
137+
138+
protected function getAvailableLocales(): Collection
139+
{
140+
$localesByDirectories = collect($this->filesystem->directories($this->directoryFrom()))
141+
->map(function (string $path) {
142+
return $this->filesystem->name($path);
143+
});
144+
145+
$localesByFiles = collect($this->filesystem->files($this->directoryFrom()))
146+
->map(function (SplFileInfo $splFileInfo) {
147+
return str_replace('.'.$splFileInfo->getExtension(), '', $splFileInfo->getFilename());
148+
});
149+
150+
return $localesByDirectories->intersect($localesByFiles)->values();
151+
}
152+
153+
protected function isAll(): bool
154+
{
155+
return $this->option('all');
156+
}
157+
158+
protected function directoryFrom(): string
159+
{
160+
return base_path('vendor/coderello/laravel-nova-lang/resources/lang');
161+
}
162+
163+
protected function directoryNovaSource(): string
164+
{
165+
return base_path('vendor/laravel/nova/resources/lang');
166+
}
167+
}

src/Providers/LaravelNovaLangServiceProvider.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Coderello\LaravelNovaLang\Commands\NovaLangPublish;
66
use Coderello\LaravelNovaLang\Commands\NovaLangMissing;
77
use Coderello\LaravelNovaLang\Commands\NovaLangStats;
8+
use Coderello\LaravelNovaLang\Commands\NovaLangReorder;
89
use Illuminate\Support\ServiceProvider;
910

1011
class LaravelNovaLangServiceProvider extends ServiceProvider
@@ -27,18 +28,20 @@ public function boot()
2728
public function register()
2829
{
2930
$this->app->singleton('command.publish.nova-lang', NovaLangPublish::class);
30-
31+
3132
$this->commands([
3233
'command.publish.nova-lang',
3334
]);
34-
35+
3536
if (config('app.debug')) {
3637
$this->app->singleton('command.missing.nova-lang', NovaLangMissing::class);
3738
$this->app->singleton('command.stats.nova-lang', NovaLangStats::class);
38-
39+
$this->app->singleton('command.reorder.nova-lang', NovaLangReorder::class);
40+
3941
$this->commands([
4042
'command.missing.nova-lang',
4143
'command.stats.nova-lang',
44+
'command.reorder.nova-lang',
4245
]);
4346
}
4447
}

0 commit comments

Comments
 (0)