Skip to content

Commit fcef2fd

Browse files
committed
Add option to ignore certain keys, which is especially helpful for custom validation messages which are checked by laravels validator
1 parent ab812d8 commit fcef2fd

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

config/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
'dump_translations' => env('DUMP_TRANSLATIONS', false),
55
'dumper' => \Bambamboole\LaravelTranslationDumper\TranslationDumper::class,
66
'dump_prefix' => 'x-',
7+
'ignore_keys' => [
8+
'validation.custom',
9+
],
710
];

src/DumpingTranslator.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ class DumpingTranslator implements TranslatorInterface
1111
public function __construct(
1212
private readonly TranslatorInterface $translator,
1313
private readonly TranslationDumperInterface $translationDumper,
14-
) {
15-
}
14+
private readonly array $ignoreKeys = [],
15+
) {}
1616

1717
public function get($key, array $replace = [], $locale = null, $fallback = true)
1818
{
1919
$translation = $this->translator->get($key, $replace, $locale, $fallback);
20-
if ($translation === $key) {
20+
if ($translation === $key && ! $this->shouldBeIgnored($key)) {
2121
$this->keysWithMissingTranslations[] = $key;
2222
}
2323

@@ -53,4 +53,15 @@ public function __destruct()
5353

5454
$this->translationDumper->dump($this->keysWithMissingTranslations);
5555
}
56+
57+
private function shouldBeIgnored(string $key): bool
58+
{
59+
foreach ($this->ignoreKeys as $ignoreKey) {
60+
if (str_contains($key, $ignoreKey)) {
61+
return true;
62+
}
63+
}
64+
65+
return false;
66+
}
5667
}

src/LaravelTranslationDumperServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function register(): void
4545
static fn (Translator $translator, $app) => new DumpingTranslator(
4646
$translator,
4747
$app->make(TranslationDumperInterface::class),
48+
$app->make(Repository::class)->get('translation.ignore_keys'),
4849
),
4950
);
5051
}

src/TranslationDumper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ public function __construct(
1212
private readonly string $languageFilePath,
1313
private string $locale,
1414
private readonly string $dumpPrefix = 'x-',
15-
) {
16-
}
15+
) {}
1716

1817
public function setLocale(string $locale): void
1918
{

0 commit comments

Comments
 (0)