Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion config/translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
],
],

Expand Down
4 changes: 1 addition & 3 deletions src/Commands/ClearCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -17,7 +15,7 @@ public function handle(): int
{
Translator::clearCache();

info('Cache cleared');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why note prompt info function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before:

image

Now:

image

For more highlighting the message.

$this->components->info('Translator cache cleared.');

return self::SUCCESS;
}
Expand Down
101 changes: 72 additions & 29 deletions src/Commands/MissingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using progress is not really relevant here because setTranslations will add all missing translations in a single write. Doing a for-loop will just make it less performant.

Copy link
Contributor Author

@sajjadhossainshohag sajjadhossainshohag Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that, but installing the package in my big project took a lot of time. So, that's why I added a progress bar for knowing about progress.

We can show progress bar with the option, like --without-progress or with-progress. Please let me know your opinion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, but the part taking time is getMissingTranslations not setTranslations.
I should certainly add a progress callback to getMissingTranslations so it could be displayed it the terminal. I'm gonna look at it this week, thx for the feedback!

}
}