-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace CodingSocks\LostInTranslation; | ||
|
||
use Illuminate\Support\Facades\Lang; | ||
use Symfony\Component\Finder\SplFileInfo; | ||
|
||
class MissingTranslationFileVisitor | ||
{ | ||
/** @var string[] Buffer for valid arguments. */ | ||
protected $translations = []; | ||
|
||
/** @var string[] Buffer for invalid arguments. */ | ||
protected $errors = []; | ||
|
||
/** @var string */ | ||
protected $locale; | ||
|
||
/** @var \CodingSocks\LostInTranslation\LostInTranslation */ | ||
protected $lit; | ||
|
||
/** | ||
* @param $locale | ||
* @param $lit | ||
*/ | ||
public function __construct($locale, $lit) | ||
{ | ||
$this->locale = $locale; | ||
$this->lit = $lit; | ||
} | ||
|
||
|
||
public function __invoke(SplFileInfo $file) | ||
{ | ||
$nodes = $this->lit->findInFile($file); | ||
|
||
$translationKeys = $this->resolveFirstArgs($nodes); | ||
|
||
foreach ($translationKeys as $key) { | ||
if (!Lang::has($key, $this->locale)) { | ||
// TODO: find a better way to check uniqueness | ||
$this->translations[] = $key; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param array $nodes | ||
* @return array | ||
*/ | ||
protected function resolveFirstArgs(array $nodes): array | ||
{ | ||
$translationKeys = []; | ||
foreach ($nodes as $node) { | ||
try { | ||
if (($key = $this->lit->resolveFirstArg($node)) !== null) { | ||
$translationKeys[] = $key; | ||
} | ||
} catch (NonStringArgumentException $e) { | ||
$this->errors[] = "skipping dynamic language key: `{$e->argument}`"; | ||
} | ||
} | ||
return array_unique($translationKeys); | ||
} | ||
|
||
public function getTranslations(): array | ||
{ | ||
return $this->translations; | ||
} | ||
|
||
public function getErrors(): array | ||
{ | ||
return $this->errors; | ||
} | ||
} |