Skip to content

Commit

Permalink
update commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbs committed Dec 3, 2021
1 parent f76029e commit afb07f8
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 16 deletions.
64 changes: 64 additions & 0 deletions dev/Commands/NovaLangCleanup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Coderello\LaravelNovaLang\Commands;

class NovaLangCleanup extends AbstractDevCommand
{
protected const MISSING_TEXT = '<MISSING>';
protected const REMOVED_MISSING_KEYS = '%d missing or blank translation keys for "%s" locale were removed.';
protected const NO_MISSING_KEYS = '"%s" locale has no missing or blank translation keys.';

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cleanup
{locales? : Comma-separated list of languages}
{--all : Output all languages}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Remove missing or blank keys from Nova language files.';

/**
* Handle the command for a given locale.
*
* @param string $locale
* @return void
*/
protected function handleLocale(string $locale): void
{
$inputFile = $this->directoryFrom("$locale.json");

if (! $this->availableLocales->contains($locale)) {
$this->warn(sprintf(static::LOCALE_FILE_DOES_NOT_EXIST, $locale));

return;
}

$inputKeys = $this->loadJson($inputFile);

$outputKeys = array_filter($inputKeys, fn ($text) => ! empty(trim($text)) && $text !== static::MISSING_TEXT);

$missingKeys = count($inputKeys) - count($outputKeys);

$outputFile = $inputFile;

if ($missingKeys > 0) {
$this->saveJson($outputFile, $outputKeys);

$this->info(sprintf(static::REMOVED_MISSING_KEYS, $missingKeys, $locale, $outputFile));
} else {
$this->info(sprintf(static::NO_MISSING_KEYS, $locale));
}
}

protected function afterHandle()
{
//
}
}
4 changes: 2 additions & 2 deletions dev/Commands/NovaLangCountry.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class NovaLangCountry extends AbstractDevCommand
{
protected const SAVED_COUNTRY_NAMES = 'Country names for "%s" locale have been added to [%s].';
protected const RUN_MISSING_COMMAND = 'Country names were not found for %d keys. These were not added to the file. Run the command `php nova-lang missing` to add them.';
protected const RUN_MISSING_COMMAND = 'Country names were not found for %d keys, which were not added to the file. Run the command `php nova-lang missing %s` to add them.';
protected const COUNTRY_NAMES_NOT_FOUND = 'Country names could not be found for "%s" locale. The file was not updated.';

protected const CLDR_URL = 'https://github.com/unicode-org/cldr-json/raw/main/cldr-json/cldr-localenames-modern/main/%s/territories.json';
Expand Down Expand Up @@ -69,7 +69,7 @@ protected function handleLocale(string $locale): void
$this->info(sprintf(static::SAVED_COUNTRY_NAMES, $locale, $outputFile));

if ($untranslated > 0) {
$this->warn(' ' . sprintf(static::RUN_MISSING_COMMAND, $untranslated, $outputFile));
$this->warn(' ' . sprintf(static::RUN_MISSING_COMMAND, $untranslated, $locale));
$this->newLine();
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion dev/Commands/NovaLangReorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class NovaLangReorder extends AbstractDevCommand
{
protected const KEYS_OUT_OF_ORDER = '%d translation keys for "%s" locale were out of order. The updated file has been output to [%s].';
protected const NO_KEYS_OUT_OF_ORDER = '"%s" locale has no translation keys out of order.';
protected const RUN_MISSING_COMMAND = '%d translation keys for "%s" locale were missing. Run the command `php nova-lang missing` to add them.';
protected const RUN_MISSING_COMMAND = '%d translation keys for "%s" locale were missing. Run the command `php nova-lang missing %2$s` to add them.';

/**
* The name and signature of the console command.
Expand Down
35 changes: 22 additions & 13 deletions dev/Commands/NovaLangStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function handle()

$contributorsTable = $contributors->map(function ($localeStat, $locale) use ($sourceCount) {
$percent = $this->getPercent($localeStat['complete'], $sourceCount);
$icon = $this->getPercentIcon($localeStat['complete'], $percent);
$icon = $this->getPercentBadge($localeStat['complete'], $percent);

$contributors = implode(', ', array_map(function ($contributor) {
if ($contributor == '(unknown)') {
Expand All @@ -156,10 +156,17 @@ public function handle()

$sourceComplete = $sourceCount * $languagesCount;
$percent = $this->getPercent($translatedCount, $sourceComplete);
$countIcon = $this->getPercentIcon($languagesCount);
$icon = $this->getPercentIcon($translatedCount, $percent);
$countIcon = $this->getTextBadge($languagesCount);
$icon = $this->getPercentBadge($translatedCount, $percent);

$totals = sprintf('Total languages ![%s](%s) ', $languagesCount, $countIcon) . PHP_EOL .
$composer = $this->loadJson($this->basePath('composer.lock'))['packages-dev'];
$package = array_filter($composer, fn ($package) => $package['name'] == 'laravel/nova');
$novaVersion = array_shift($package)['version'];
$versionIcon = $this->getTextBadge($novaVersion);

$totals =
sprintf('Latest Nova version ![%s](%s) ', $novaVersion, $versionIcon) . PHP_EOL .
sprintf('Total languages ![%s](%s) ', $languagesCount, $countIcon) . PHP_EOL .
sprintf('Total lines translated ![%s (%s%%)](%s)', number_format($translatedCount), $percent, $icon);

$header = '## Available Languages' . PHP_EOL . PHP_EOL.
Expand All @@ -186,11 +193,12 @@ public function handle()
$contributorsList = $contributors->map(function ($localeStat, $locale) use ($sourceCount) {
$percent = $this->getPercent($localeStat['complete'], $sourceCount);

return sprintf('* `%s` %s &middot; **%d (%s%%)**', str_replace('-', '', $locale), $localeStat['name'], $localeStat['complete'], $percent);
return sprintf('* `%s` %s &middot; **%d** (%s%%)', str_replace('-', '', $locale), $localeStat['name'], $localeStat['complete'], $percent);
});

$totals = sprintf('Total languages **%s** ', $languagesCount) . PHP_EOL .
sprintf('Total lines translated **%s (%s%%)**', number_format($translatedCount), $percent);
$totals = sprintf('Current Nova version **%s** ', $novaVersion) . PHP_EOL .
sprintf('Total languages **%s** ', $languagesCount) . PHP_EOL .
sprintf('Total lines translated **%s** (%s%%)', number_format($translatedCount), $percent);

$header = '### Available Languages' . PHP_EOL . PHP_EOL .
$totals . PHP_EOL;
Expand All @@ -201,7 +209,7 @@ public function handle()

$originalContents = $this->loadText($outputFile);

$contents = preg_replace('/(.+)## Available Languages.+/sm', '$1' . $contents, $originalContents);
$contents = preg_replace('/^#+ Available Languages.+/sm', '$1' . $contents, $originalContents);

$this->saveText($outputFile, $contents);

Expand All @@ -217,12 +225,8 @@ protected function getPercent(int $complete, int $total): float
return $complete > $total ? 100 : round(($complete / $total) * 100, 1);
}

protected function getPercentIcon($complete, $percent = null): string
protected function getPercentBadge(float $complete, float $percent): string
{
if (is_null($percent)) {
return sprintf('https://img.shields.io/badge/%d-gray?style=flat-square', $complete);
}

$colors = [
1 => 'red',
85 => 'orange',
Expand All @@ -244,6 +248,11 @@ protected function getPercentIcon($complete, $percent = null): string
return sprintf('https://img.shields.io/badge/%s-%s%%25-%s?style=flat-square', $complete, $percent, $color);
}

protected function getTextBadge(string $text): string
{
return sprintf('https://img.shields.io/badge/%s-gray?style=flat-square', $text);
}

protected function getAvailableLocales(): Collection
{
$localesByDirectories = collect($this->filesystem->directories($this->directoryFrom()))
Expand Down
1 change: 1 addition & 0 deletions nova-lang
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ $artisan->setName('Laravel Nova Language');

$artisan->resolve(Commands\NovaLangCountry::class);
$artisan->resolve(Commands\NovaLangMissing::class);
$artisan->resolve(Commands\NovaLangCleanup::class);
$artisan->resolve(Commands\NovaLangReorder::class);
$artisan->resolve(Commands\NovaLangStats::class);

Expand Down

0 comments on commit afb07f8

Please sign in to comment.