generated from fidum/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds command to find missing translations (#10)
* Adds command to find missing translations * wip
- Loading branch information
Showing
58 changed files
with
886 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Collections; | ||
|
||
use Fidum\LaravelTranslationLinter\Contracts\Collections\ApplicationFileCollection as ApplicationFileCollectionContract; | ||
use Fidum\LaravelTranslationLinter\Data\ApplicationFileObject; | ||
use Illuminate\Support\Collection; | ||
|
||
/** | ||
* @method self __construct(ApplicationFileObject[] $items = null) | ||
* @method self push(ApplicationFileObject $object) | ||
*/ | ||
class ApplicationFileCollection extends Collection implements ApplicationFileCollectionContract | ||
{ | ||
public function containsKey(string $key): bool | ||
{ | ||
return $this->some(function (ApplicationFileObject $object) use ($key) { | ||
return $object->namespaceHintedKey === $key; | ||
}); | ||
} | ||
|
||
public function doesntContainKey(string $key): bool | ||
{ | ||
return ! $this->containsKey($key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Collections\Concerns; | ||
|
||
use Fidum\LaravelTranslationLinter\Contracts\Collections\FieldCollection as FieldCollectionContract; | ||
use Illuminate\Support\Str; | ||
|
||
trait CollectsFields | ||
{ | ||
public function enabled(): FieldCollectionContract | ||
{ | ||
return $this->filter()->keys(); | ||
} | ||
|
||
public function headers(): array | ||
{ | ||
return $this->enabled() | ||
->map(fn ($v) => Str::headline($v)) | ||
->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Collections\Concerns; | ||
|
||
use Fidum\LaravelTranslationLinter\Contracts\Filters\Filter; | ||
use Fidum\LaravelTranslationLinter\Data\ResultObject; | ||
use http\Exception\InvalidArgumentException; | ||
|
||
trait CollectsFilters | ||
{ | ||
public function shouldReport(ResultObject $object): bool | ||
{ | ||
return $this->every(function (string $filterClass) use ($object) { | ||
$interface = Filter::class; | ||
|
||
if (is_subclass_of($filterClass, $interface)) { | ||
/** @var Filter $filter */ | ||
$filter = app($filterClass); | ||
|
||
return $filter->shouldReport($object); | ||
} | ||
|
||
throw new InvalidArgumentException("Filter [$filterClass] needs to implement [$interface]."); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Collections; | ||
|
||
use Fidum\LaravelTranslationLinter\Collections\Concerns\CollectsFields; | ||
use Fidum\LaravelTranslationLinter\Contracts\Collections\MissingFieldCollection as MissingFieldCollectionContract; | ||
use Illuminate\Support\Collection; | ||
|
||
class MissingFieldCollection extends Collection implements MissingFieldCollectionContract | ||
{ | ||
use CollectsFields; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Collections; | ||
|
||
use Fidum\LaravelTranslationLinter\Collections\Concerns\CollectsFilters; | ||
use Fidum\LaravelTranslationLinter\Contracts\Collections\MissingFilterCollection as MissingFilterCollectionContract; | ||
use Illuminate\Support\Collection; | ||
|
||
class MissingFilterCollection extends Collection implements MissingFilterCollectionContract | ||
{ | ||
use CollectsFilters; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Commands; | ||
|
||
use Fidum\LaravelTranslationLinter\Collections\ResultObjectCollection; | ||
use Fidum\LaravelTranslationLinter\Contracts\Collections\MissingFieldCollection; | ||
use Fidum\LaravelTranslationLinter\Contracts\Collections\MissingFilterCollection; | ||
use Fidum\LaravelTranslationLinter\Contracts\Linters\MissingTranslationLinter; | ||
use Fidum\LaravelTranslationLinter\Data\ResultObject; | ||
use Fidum\LaravelTranslationLinter\Filters\IgnoreKeysFromMissingBaselineFileFilter; | ||
use Fidum\LaravelTranslationLinter\Writers\MissingBaselineFileWriter; | ||
use Illuminate\Console\Command; | ||
|
||
class MissingCommand extends Command | ||
{ | ||
public $signature = 'translation:missing | ||
{paths?* : One or more absolute paths to files you specifically want to scan for missing keys.} | ||
{--b|generate-baseline : Generate a baseline file from the missing keys.}'; | ||
|
||
public $description = 'Finds unused language keys.'; | ||
|
||
public function handle( | ||
MissingBaselineFileWriter $writer, | ||
MissingFieldCollection $fields, | ||
MissingFilterCollection $filters, | ||
MissingTranslationLinter $linter, | ||
): int { | ||
$baseline = (bool) $this->option('generate-baseline'); | ||
$results = $linter->execute(); | ||
|
||
if ($baseline) { | ||
$results = $results->whereShouldReport($filters); | ||
|
||
$writer->execute($results); | ||
|
||
$this->components->info("Baseline file written with {$results->count()} translation keys."); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
$filters->push(IgnoreKeysFromMissingBaselineFileFilter::class); | ||
|
||
$results = $results | ||
->when($this->argument('paths'), function (ResultObjectCollection $items, array $files) { | ||
return $items->filter(fn (ResultObject $object) => in_array($object->file->getPathname(), $files)); | ||
}) | ||
->whereShouldReport($filters); | ||
|
||
if ($results->isEmpty()) { | ||
$this->components->info('No missing translations found!'); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
$this->components->error(sprintf('%d missing translations found', $results->count())); | ||
$this->table($fields->headers(), $results->toCommandTableOutputArray($fields)); | ||
|
||
return self::FAILURE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Contracts\Collections; | ||
|
||
use Fidum\LaravelTranslationLinter\Data\ApplicationFileObject; | ||
use Illuminate\Contracts\Support\Arrayable; | ||
use Illuminate\Support\Enumerable; | ||
|
||
/** | ||
* @method self __construct(ApplicationFileObject[] $items = null) | ||
* @method self push(ApplicationFileObject $item) | ||
*/ | ||
interface ApplicationFileCollection extends Arrayable, Enumerable | ||
{ | ||
public function containsKey(string $key): bool; | ||
|
||
public function doesntContainKey(string $key): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Contracts\Collections; | ||
|
||
interface MissingFieldCollection extends FieldCollection {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Contracts\Collections; | ||
|
||
interface MissingFilterCollection extends FilterCollection {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Contracts\Linters; | ||
|
||
interface MissingTranslationLinter extends TranslationLinter {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Contracts\Linters; | ||
|
||
use Fidum\LaravelTranslationLinter\Contracts\Collections\ResultObjectCollection; | ||
|
||
interface TranslationLinter | ||
{ | ||
public function execute(): ResultObjectCollection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.