From 8b2f23f024fc11d1cc2f79390f8f4928c44ad190 Mon Sep 17 00:00:00 2001 From: Sajjad Hossain Shohag <63788037+sajjadhossainshohagbd@users.noreply.github.com> Date: Tue, 10 Dec 2024 05:06:44 +0000 Subject: [PATCH 1/5] Cache path moved in storage folder --- config/translator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/translator.php b/config/translator.php index 811ee84..8985810 100644 --- a/config/translator.php +++ b/config/translator.php @@ -151,7 +151,7 @@ * To speed up detection, all the results of the scan will be stored in a file. * Feel free to change the path if needed. */ - 'cache_path' => base_path('.translator.cache'), + 'cache_path' => storage_path('.translator.cache'), ], ], From 546bc2cf4ad96a647f8b927ee32b2e1888e243de Mon Sep 17 00:00:00 2001 From: Sajjad Hossain Shohag <63788037+sajjadhossainshohagbd@users.noreply.github.com> Date: Tue, 10 Dec 2024 10:08:28 +0000 Subject: [PATCH 2/5] Added progress bar and improve messages --- src/Commands/ClearCacheCommand.php | 4 +- src/Commands/MissingCommand.php | 100 +++++++++++++++++++++-------- 2 files changed, 73 insertions(+), 31 deletions(-) diff --git a/src/Commands/ClearCacheCommand.php b/src/Commands/ClearCacheCommand.php index 3318201..4061283 100644 --- a/src/Commands/ClearCacheCommand.php +++ b/src/Commands/ClearCacheCommand.php @@ -5,8 +5,6 @@ use Elegantly\Translator\Facades\Translator; use Illuminate\Console\Command; -use function Laravel\Prompts\info; - class ClearCacheCommand extends Command { public $signature = 'translator:clear-cache'; @@ -17,7 +15,7 @@ public function handle(): int { Translator::clearCache(); - info('Cache cleared'); + $this->components->info('Translator cache cleared.'); return self::SUCCESS; } diff --git a/src/Commands/MissingCommand.php b/src/Commands/MissingCommand.php index 7f9f9b6..63ee233 100644 --- a/src/Commands/MissingCommand.php +++ b/src/Commands/MissingCommand.php @@ -6,7 +6,7 @@ use function Laravel\Prompts\info; use function Laravel\Prompts\intro; -use function Laravel\Prompts\note; +use function Laravel\Prompts\progress; use function Laravel\Prompts\table; class MissingCommand extends TranslatorCommand implements PromptsForMissingInput @@ -21,41 +21,85 @@ public function handle(): int $sync = (bool) $this->option('sync'); $translator = $this->getTranslator(); - $missing = $translator->getMissingTranslations($locale); - intro('Using driver: '.$translator->driver::class); - - note(count($missing).' missing keys detected.'); - - table( - headers: ['Key', 'Count', 'Files'], - rows: collect($missing) - ->map(function ($value, $key) { - return [ - str($key)->limit(20)->value(), - (string) $value['count'], - implode("\n", - array_map( - fn ($file) => str($file)->after(base_path()), - $value['files'], - ) - ), - ]; - })->values()->all() - ); + intro('Using driver: ' . $translator->driver::class); - if ($sync) { + $missingCount = count($missing); - $translator->setTranslations( - locale: $locale, - values: array_map(fn () => null, $missing) - ); + if ($missingCount === 0) { + info('No missing keys found for locale: ' . $locale); + return self::SUCCESS; + } - info(count($missing).' missing keys added to the driver.'); + $this->components->info("{$missingCount} missing keys detected."); + $this->newLine(); + if (!$sync) { + table( + headers: ['Key', 'Total Count', 'Files'], + rows: $this->formatMissingKeys($missing) + ); + } + + if ($sync) { + $this->syncMissingKeysWithProgress($translator, $locale, $missing); } return self::SUCCESS; } + + /** + * Format missing keys for display in the table. + * + * @param array $missing + * @return array + */ + private function formatMissingKeys(array $missing): array + { + return collect($missing) + ->map(fn($value, $key) => [ + str($key)->limit(20)->value(), + (string) $value['count'], + implode("\n", $this->formatFiles($value['files'])), + ]) + ->values() + ->all(); + } + + /** + * Format file paths for display. + * + * @param array $files + * @return array + */ + private function formatFiles(array $files): array + { + return array_map( + fn($file) => str($file)->after(base_path())->value(), + $files + ); + } + + /** + * Sync missing keys with the translator driver, showing a progress bar. + * + * @param object $translator + * @param string $locale + * @param array $missing + * @return void + */ + private function syncMissingKeysWithProgress($translator, string $locale, array $missing): void + { + $progress = progress(label: 'Syncing...', steps: count($missing)); + $progress->start(); + + foreach ($missing as $key => $value) { + $translator->setTranslation($locale, $key, null); + $progress->advance(); + } + + $progress->finish(); + $this->components->info(count($missing) . ' missing keys have been synced.'); + } } From a6b1d7b03da386149a776a9090b5ab5cd4e9136c Mon Sep 17 00:00:00 2001 From: Sajjad Hossain Shohag <63788037+sajjadhossainshohagbd@users.noreply.github.com> Date: Tue, 17 Dec 2024 03:47:33 +0000 Subject: [PATCH 3/5] fix readme path --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 91bced3..36ebf76 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ composer require elegantly/laravel-translator --dev Add the following line to your `.gitignore` file: ``` -.translator.cache +storage/.translator.cache ``` Publish the configuration file: From fa9ff1e5d7d1a8af4f41a51ba3d95ee256f9b953 Mon Sep 17 00:00:00 2001 From: Sajjad Hossain Shohag <63788037+sajjadhossainshohagbd@users.noreply.github.com> Date: Tue, 17 Dec 2024 03:58:50 +0000 Subject: [PATCH 4/5] Removed prompt info func --- src/Commands/MissingCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Commands/MissingCommand.php b/src/Commands/MissingCommand.php index 63ee233..7be8ba8 100644 --- a/src/Commands/MissingCommand.php +++ b/src/Commands/MissingCommand.php @@ -4,7 +4,6 @@ use Illuminate\Contracts\Console\PromptsForMissingInput; -use function Laravel\Prompts\info; use function Laravel\Prompts\intro; use function Laravel\Prompts\progress; use function Laravel\Prompts\table; @@ -28,7 +27,7 @@ public function handle(): int $missingCount = count($missing); if ($missingCount === 0) { - info('No missing keys found for locale: ' . $locale); + $this->components->info('No missing keys found for locale: ' . $locale); return self::SUCCESS; } From 4ccae6a342d64c5a92bb37cc416c01bb4ff07ca8 Mon Sep 17 00:00:00 2001 From: Sajjad Hossain Shohag <63788037+sajjadhossainshohagbd@users.noreply.github.com> Date: Tue, 17 Dec 2024 18:00:48 +0600 Subject: [PATCH 5/5] Rollback MissingCommand.php --- src/Commands/MissingCommand.php | 101 +++++++++----------------------- 1 file changed, 29 insertions(+), 72 deletions(-) diff --git a/src/Commands/MissingCommand.php b/src/Commands/MissingCommand.php index 7be8ba8..7f9f9b6 100644 --- a/src/Commands/MissingCommand.php +++ b/src/Commands/MissingCommand.php @@ -4,8 +4,9 @@ use Illuminate\Contracts\Console\PromptsForMissingInput; +use function Laravel\Prompts\info; use function Laravel\Prompts\intro; -use function Laravel\Prompts\progress; +use function Laravel\Prompts\note; use function Laravel\Prompts\table; class MissingCommand extends TranslatorCommand implements PromptsForMissingInput @@ -20,85 +21,41 @@ public function handle(): int $sync = (bool) $this->option('sync'); $translator = $this->getTranslator(); - $missing = $translator->getMissingTranslations($locale); - - intro('Using driver: ' . $translator->driver::class); - - $missingCount = count($missing); - if ($missingCount === 0) { - $this->components->info('No missing keys found for locale: ' . $locale); - return self::SUCCESS; - } - - $this->components->info("{$missingCount} missing keys detected."); - $this->newLine(); + $missing = $translator->getMissingTranslations($locale); - if (!$sync) { - table( - headers: ['Key', 'Total Count', 'Files'], - rows: $this->formatMissingKeys($missing) - ); - } + intro('Using driver: '.$translator->driver::class); + + note(count($missing).' missing keys detected.'); + + table( + headers: ['Key', 'Count', 'Files'], + rows: collect($missing) + ->map(function ($value, $key) { + return [ + str($key)->limit(20)->value(), + (string) $value['count'], + implode("\n", + array_map( + fn ($file) => str($file)->after(base_path()), + $value['files'], + ) + ), + ]; + })->values()->all() + ); if ($sync) { - $this->syncMissingKeysWithProgress($translator, $locale, $missing); - } - - return self::SUCCESS; - } - - /** - * Format missing keys for display in the table. - * - * @param array $missing - * @return array - */ - private function formatMissingKeys(array $missing): array - { - return collect($missing) - ->map(fn($value, $key) => [ - str($key)->limit(20)->value(), - (string) $value['count'], - implode("\n", $this->formatFiles($value['files'])), - ]) - ->values() - ->all(); - } - /** - * Format file paths for display. - * - * @param array $files - * @return array - */ - private function formatFiles(array $files): array - { - return array_map( - fn($file) => str($file)->after(base_path())->value(), - $files - ); - } + $translator->setTranslations( + locale: $locale, + values: array_map(fn () => null, $missing) + ); - /** - * Sync missing keys with the translator driver, showing a progress bar. - * - * @param object $translator - * @param string $locale - * @param array $missing - * @return void - */ - private function syncMissingKeysWithProgress($translator, string $locale, array $missing): void - { - $progress = progress(label: 'Syncing...', steps: count($missing)); - $progress->start(); + info(count($missing).' missing keys added to the driver.'); - foreach ($missing as $key => $value) { - $translator->setTranslation($locale, $key, null); - $progress->advance(); } - $progress->finish(); - $this->components->info(count($missing) . ' missing keys have been synced.'); + return self::SUCCESS; } }