Skip to content
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

Excluding keys #34

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
8 changes: 0 additions & 8 deletions config/config.php

This file was deleted.

20 changes: 20 additions & 0 deletions config/translation-checker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
return [

/*
|--------------------------------------------------------------------------
| Laravel Translation Checker
|--------------------------------------------------------------------------
|
| This package allows you to check your Laravel translations for missing
| keys, unused keys and unused translations.
| You can ignore some keys by adding them to the 'ignore' array.
Comment on lines +9 to +11

Choose a reason for hiding this comment

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

I know this is the description i used in my PR but currently the package let's you find missing keys but don't have any functionality related to "unused keys/translations". When I opened the PR I just pasted an example description without paying too much attention.
Maybe you can remove that part to better reflect the current functionality of the package.

|
*/


'excluded_keys' => [
//
],

];
68 changes: 46 additions & 22 deletions src/Console/Commands/CheckIfTranslationsAreAllThereCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Larswiegers\LaravelTranslationsChecker\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use RecursiveDirectoryIterator;
Expand All @@ -15,7 +16,7 @@ class CheckIfTranslationsAreAllThereCommand extends Command
*
* @var string
*/
protected $signature = 'translations:check {--directory=} {--excludedDirectories=none}';
protected $signature = 'translations:check {--directory=} {--excludedDirectories=none} {--excludedKeys=none}';

/**
* The console command description.
Expand All @@ -33,6 +34,7 @@ class CheckIfTranslationsAreAllThereCommand extends Command
* @var array
*/
public array $realLines = [];
private Collection $excludedKeys;

/**
* Create a new command instance.
Expand All @@ -52,20 +54,16 @@ public function __construct()
public function handle()
{
$directory = $this->option('directory') ?: app()->langPath();

if ($this->option('excludedDirectories') === 'none') {
$this->excludedDirectories = [];
} elseif ($this->option('excludedDirectories')) {
$this->excludedDirectories = explode(',', $this->option('excludedDirectories'));
} else {
$this->excludedDirectories = [];
}

if (!$this->checkIfDirectoryExists($directory)) {
$this->error('The passed directory (' . $directory . ') does not exist.');
return $this::FAILURE;
}

$this->excludedDirectories = $this->getExcludedDirectories();

$this->excludedKeys = $this->getExcludedKeys();


$languages = $this->getLanguages($directory);
$missingFiles = [];

Expand Down Expand Up @@ -106,24 +104,25 @@ public function handle()

$exists = $this->translationExistsAsJsonOrAsSubDir($directory, $language, $fileKey, $keyWithoutFile);

if ($this->isDirInExcludedDirectories($language)) {
if ($this->isDirInExcludedDirectories($language) ||
$exists ||
$this->excludedKeys->contains($keyWithoutFile)
) {
continue;
}
if (!$exists) {
$fileName = Str::replace(['.php', '.json'], '', $fileKey);

foreach($languages as $checkingLanguage) {
if(Str::contains($fileName, $checkingLanguage)) {
$fileName = str_replace($checkingLanguage, '', $fileName);
}
}
$fileName = Str::replace(['.php', '.json'], '', $fileKey);

if(Str::contains($fileKey, $languages)) {
$missing[] = $language . '.' . $keyWithoutFile;
}else {
$missing[] = $language . '.' . $fileName . '.' . $keyWithoutFile;
foreach($languages as $checkingLanguage) {
if(Str::contains($fileName, $checkingLanguage)) {
$fileName = str_replace($checkingLanguage, '', $fileName);
}
}

if(Str::contains($fileKey, $languages)) {
$missing[] = $language . '.' . $keyWithoutFile;
}else {
$missing[] = $language . '.' . $fileName . '.' . $keyWithoutFile;
}
}
}
Expand Down Expand Up @@ -246,4 +245,29 @@ public function translationExistsAsJsonOrAsSubDir($directory, $language, string
$existsAsJSONValue = array_key_exists($directory . DIRECTORY_SEPARATOR . $language . '.' . $fileKeyWithoutLangComponent . '**' . $keyWithoutFile, $this->realLines);
return $existsAsSubDirValue || $existsAsJSONValue;
}

/**
* @return array
*/
public function getExcludedDirectories(): array
{
if ($this->option('excludedDirectories') === 'none') {
return [];
} elseif ($this->option('excludedDirectories')) {
return explode(',', $this->option('excludedDirectories'));
} else {
return [];
}
}

private function getExcludedKeys(): Collection
{
if ($this->option('excludedKeys') === 'none') {
return collect(config('translation-checker.excluded_keys'));
} elseif ($this->option('excludedKeys')) {
return collect(explode(',', $this->option('excludedKeys')));
} else {
return collect();
}
}
}
4 changes: 2 additions & 2 deletions src/LaravelTranslationsCheckerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function boot()

if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/config.php' => config_path('laravel-translations-checker.php'),
__DIR__ . '/../config/translation-checker.php' => config_path('laravel-translations-checker.php'),
], 'config');

// Publishing the views.
Expand Down Expand Up @@ -53,7 +53,7 @@ public function boot()
public function register()
{
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-translations-checker');
$this->mergeConfigFrom(__DIR__ . '/../config/translation-checker.php', 'laravel-translations-checker');

// Register the main class to use with the facade
$this->app->singleton('laravel-translations-checker', function () {
Expand Down
52 changes: 52 additions & 0 deletions tests/Tests/Unit/Console/Commands/ConfigExcludesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Console\Commands;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use Tests\TestCase;

final class ConfigExcludesTest extends TestCase
{

public string $excludesDir = "tests/resources/lang/excluded_keys/";
public function test_it_is_okay_if_we_dont_pass_in_excluded_keys()
{
$dir = $this->excludesDir . 'no_excludes';
$command = $this->artisan('translations:check', [
'--directory' => $dir,
'--excludedKeys' => []
]);

$command->assertExitCode(0);
}

public function test_it_excludes_a_key_if_it_is_passed_in()
{
$dir = $this->excludesDir . 'exclude_one_missing_key';
$command = $this->artisan('translations:check', [
'--directory' => $dir,
'--excludedKeys' => implode(',', [
'test_key'
])
]);

$command->assertExitCode(0);
}

public function test_it_can_get_them_from_config()
{
$dir = $this->excludesDir . 'exclude_one_missing_key';

Config::set('translation-checker.excluded_keys', ['test_key']);

$command = $this->artisan('translations:check', [
'--directory' => $dir
]);

$command->assertExitCode(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'test_key' => 'test_value',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

return [
];
5 changes: 5 additions & 0 deletions tests/resources/lang/excluded_keys/no_excludes/en/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'test_key' => 'test_value',
];
5 changes: 5 additions & 0 deletions tests/resources/lang/excluded_keys/no_excludes/nl/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'test_key' => 'test_value',
];