|
| 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 | +} |
0 commit comments