Skip to content

Command to reset the order of translated keys #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions src/Commands/NovaLangMissing.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@ public function handle()

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

if (!in_array(':resource Details', $sourceKeys)) { // Temporary fix until laravel/nova#463 is merged
$sourceKeys = array_unique(array_merge($sourceKeys, [
'Action',
'Changes',
'Original',
'This resource no longer exists',
':resource Details',
]));
}

$availableLocales = $this->getAvailableLocales();

$requestedLocales = $this->getRequestedLocales();
Expand Down
167 changes: 167 additions & 0 deletions src/Commands/NovaLangReorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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');
}
}
9 changes: 6 additions & 3 deletions src/Providers/LaravelNovaLangServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Coderello\LaravelNovaLang\Commands\NovaLangPublish;
use Coderello\LaravelNovaLang\Commands\NovaLangMissing;
use Coderello\LaravelNovaLang\Commands\NovaLangStats;
use Coderello\LaravelNovaLang\Commands\NovaLangReorder;
use Illuminate\Support\ServiceProvider;

class LaravelNovaLangServiceProvider extends ServiceProvider
Expand All @@ -27,18 +28,20 @@ public function boot()
public function register()
{
$this->app->singleton('command.publish.nova-lang', NovaLangPublish::class);

$this->commands([
'command.publish.nova-lang',
]);

if (config('app.debug')) {
$this->app->singleton('command.missing.nova-lang', NovaLangMissing::class);
$this->app->singleton('command.stats.nova-lang', NovaLangStats::class);

$this->app->singleton('command.reorder.nova-lang', NovaLangReorder::class);

$this->commands([
'command.missing.nova-lang',
'command.stats.nova-lang',
'command.reorder.nova-lang',
]);
}
}
Expand Down