Skip to content

Commit

Permalink
Add option to ignore certain keys, which is especially helpful for cu…
Browse files Browse the repository at this point in the history
…stom validation messages which are checked by laravels validator
  • Loading branch information
bambamboole committed Jul 22, 2024
1 parent ab812d8 commit fcef2fd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
'dump_translations' => env('DUMP_TRANSLATIONS', false),
'dumper' => \Bambamboole\LaravelTranslationDumper\TranslationDumper::class,
'dump_prefix' => 'x-',
'ignore_keys' => [
'validation.custom',
],
];
17 changes: 14 additions & 3 deletions src/DumpingTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class DumpingTranslator implements TranslatorInterface
public function __construct(
private readonly TranslatorInterface $translator,
private readonly TranslationDumperInterface $translationDumper,
) {
}
private readonly array $ignoreKeys = [],
) {}

public function get($key, array $replace = [], $locale = null, $fallback = true)
{
$translation = $this->translator->get($key, $replace, $locale, $fallback);
if ($translation === $key) {
if ($translation === $key && ! $this->shouldBeIgnored($key)) {
$this->keysWithMissingTranslations[] = $key;
}

Expand Down Expand Up @@ -53,4 +53,15 @@ public function __destruct()

$this->translationDumper->dump($this->keysWithMissingTranslations);
}

private function shouldBeIgnored(string $key): bool
{
foreach ($this->ignoreKeys as $ignoreKey) {
if (str_contains($key, $ignoreKey)) {
return true;
}
}

return false;
}
}
1 change: 1 addition & 0 deletions src/LaravelTranslationDumperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function register(): void
static fn (Translator $translator, $app) => new DumpingTranslator(
$translator,
$app->make(TranslationDumperInterface::class),
$app->make(Repository::class)->get('translation.ignore_keys'),
),
);
}
Expand Down
3 changes: 1 addition & 2 deletions src/TranslationDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public function __construct(
private readonly string $languageFilePath,
private string $locale,
private readonly string $dumpPrefix = 'x-',
) {
}
) {}

public function setLocale(string $locale): void
{
Expand Down

0 comments on commit fcef2fd

Please sign in to comment.