-
-
Notifications
You must be signed in to change notification settings - Fork 3
Refactor: Align cache path with Laravel conventions #7
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
Changes from 4 commits
8b2f23f
546bc2c
a6b1d7b
fa9ff1e
4ccae6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why note prompt
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| $this->components->info('Translator cache cleared.'); | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,8 @@ | |
|
|
||
| use Illuminate\Contracts\Console\PromptsForMissingInput; | ||
|
|
||
| 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 +20,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) { | ||
| $this->components->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.'); | ||
|
||
| } | ||
| } | ||


Uh oh!
There was an error while loading. Please reload this page.