Skip to content

Commit afb07f8

Browse files
committed
update commands
1 parent f76029e commit afb07f8

File tree

5 files changed

+90
-16
lines changed

5 files changed

+90
-16
lines changed

dev/Commands/NovaLangCleanup.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Coderello\LaravelNovaLang\Commands;
4+
5+
class NovaLangCleanup extends AbstractDevCommand
6+
{
7+
protected const MISSING_TEXT = '<MISSING>';
8+
protected const REMOVED_MISSING_KEYS = '%d missing or blank translation keys for "%s" locale were removed.';
9+
protected const NO_MISSING_KEYS = '"%s" locale has no missing or blank translation keys.';
10+
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'cleanup
17+
{locales? : Comma-separated list of languages}
18+
{--all : Output all languages}';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Remove missing or blank keys from Nova language files.';
26+
27+
/**
28+
* Handle the command for a given locale.
29+
*
30+
* @param string $locale
31+
* @return void
32+
*/
33+
protected function handleLocale(string $locale): void
34+
{
35+
$inputFile = $this->directoryFrom("$locale.json");
36+
37+
if (! $this->availableLocales->contains($locale)) {
38+
$this->warn(sprintf(static::LOCALE_FILE_DOES_NOT_EXIST, $locale));
39+
40+
return;
41+
}
42+
43+
$inputKeys = $this->loadJson($inputFile);
44+
45+
$outputKeys = array_filter($inputKeys, fn ($text) => ! empty(trim($text)) && $text !== static::MISSING_TEXT);
46+
47+
$missingKeys = count($inputKeys) - count($outputKeys);
48+
49+
$outputFile = $inputFile;
50+
51+
if ($missingKeys > 0) {
52+
$this->saveJson($outputFile, $outputKeys);
53+
54+
$this->info(sprintf(static::REMOVED_MISSING_KEYS, $missingKeys, $locale, $outputFile));
55+
} else {
56+
$this->info(sprintf(static::NO_MISSING_KEYS, $locale));
57+
}
58+
}
59+
60+
protected function afterHandle()
61+
{
62+
//
63+
}
64+
}

dev/Commands/NovaLangCountry.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class NovaLangCountry extends AbstractDevCommand
99
{
1010
protected const SAVED_COUNTRY_NAMES = 'Country names for "%s" locale have been added to [%s].';
11-
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.';
11+
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.';
1212
protected const COUNTRY_NAMES_NOT_FOUND = 'Country names could not be found for "%s" locale. The file was not updated.';
1313

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

7171
if ($untranslated > 0) {
72-
$this->warn(' ' . sprintf(static::RUN_MISSING_COMMAND, $untranslated, $outputFile));
72+
$this->warn(' ' . sprintf(static::RUN_MISSING_COMMAND, $untranslated, $locale));
7373
$this->newLine();
7474
}
7575
} else {

dev/Commands/NovaLangReorder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class NovaLangReorder extends AbstractDevCommand
88
{
99
protected const KEYS_OUT_OF_ORDER = '%d translation keys for "%s" locale were out of order. The updated file has been output to [%s].';
1010
protected const NO_KEYS_OUT_OF_ORDER = '"%s" locale has no translation keys out of order.';
11-
protected const RUN_MISSING_COMMAND = '%d translation keys for "%s" locale were missing. Run the command `php nova-lang missing` to add them.';
11+
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.';
1212

1313
/**
1414
* The name and signature of the console command.

dev/Commands/NovaLangStats.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function handle()
134134

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

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

157157
$sourceComplete = $sourceCount * $languagesCount;
158158
$percent = $this->getPercent($translatedCount, $sourceComplete);
159-
$countIcon = $this->getPercentIcon($languagesCount);
160-
$icon = $this->getPercentIcon($translatedCount, $percent);
159+
$countIcon = $this->getTextBadge($languagesCount);
160+
$icon = $this->getPercentBadge($translatedCount, $percent);
161161

162-
$totals = sprintf('Total languages ![%s](%s) ', $languagesCount, $countIcon) . PHP_EOL .
162+
$composer = $this->loadJson($this->basePath('composer.lock'))['packages-dev'];
163+
$package = array_filter($composer, fn ($package) => $package['name'] == 'laravel/nova');
164+
$novaVersion = array_shift($package)['version'];
165+
$versionIcon = $this->getTextBadge($novaVersion);
166+
167+
$totals =
168+
sprintf('Latest Nova version ![%s](%s) ', $novaVersion, $versionIcon) . PHP_EOL .
169+
sprintf('Total languages ![%s](%s) ', $languagesCount, $countIcon) . PHP_EOL .
163170
sprintf('Total lines translated ![%s (%s%%)](%s)', number_format($translatedCount), $percent, $icon);
164171

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

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

192-
$totals = sprintf('Total languages **%s** ', $languagesCount) . PHP_EOL .
193-
sprintf('Total lines translated **%s (%s%%)**', number_format($translatedCount), $percent);
199+
$totals = sprintf('Current Nova version **%s** ', $novaVersion) . PHP_EOL .
200+
sprintf('Total languages **%s** ', $languagesCount) . PHP_EOL .
201+
sprintf('Total lines translated **%s** (%s%%)', number_format($translatedCount), $percent);
194202

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

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

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

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

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

220-
protected function getPercentIcon($complete, $percent = null): string
228+
protected function getPercentBadge(float $complete, float $percent): string
221229
{
222-
if (is_null($percent)) {
223-
return sprintf('https://img.shields.io/badge/%d-gray?style=flat-square', $complete);
224-
}
225-
226230
$colors = [
227231
1 => 'red',
228232
85 => 'orange',
@@ -244,6 +248,11 @@ protected function getPercentIcon($complete, $percent = null): string
244248
return sprintf('https://img.shields.io/badge/%s-%s%%25-%s?style=flat-square', $complete, $percent, $color);
245249
}
246250

251+
protected function getTextBadge(string $text): string
252+
{
253+
return sprintf('https://img.shields.io/badge/%s-gray?style=flat-square', $text);
254+
}
255+
247256
protected function getAvailableLocales(): Collection
248257
{
249258
$localesByDirectories = collect($this->filesystem->directories($this->directoryFrom()))

nova-lang

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $artisan->setName('Laravel Nova Language');
1616

1717
$artisan->resolve(Commands\NovaLangCountry::class);
1818
$artisan->resolve(Commands\NovaLangMissing::class);
19+
$artisan->resolve(Commands\NovaLangCleanup::class);
1920
$artisan->resolve(Commands\NovaLangReorder::class);
2021
$artisan->resolve(Commands\NovaLangStats::class);
2122

0 commit comments

Comments
 (0)