From fcef2fd9dd33ff6fd2eb6323145ace50958e651a Mon Sep 17 00:00:00 2001 From: bambamboole Date: Mon, 22 Jul 2024 21:59:17 +0200 Subject: [PATCH] Add option to ignore certain keys, which is especially helpful for custom validation messages which are checked by laravels validator --- config/config.php | 3 +++ src/DumpingTranslator.php | 17 ++++++++++++++--- src/LaravelTranslationDumperServiceProvider.php | 1 + src/TranslationDumper.php | 3 +-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/config/config.php b/config/config.php index d6fdc94..8e2bf45 100644 --- a/config/config.php +++ b/config/config.php @@ -4,4 +4,7 @@ 'dump_translations' => env('DUMP_TRANSLATIONS', false), 'dumper' => \Bambamboole\LaravelTranslationDumper\TranslationDumper::class, 'dump_prefix' => 'x-', + 'ignore_keys' => [ + 'validation.custom', + ], ]; diff --git a/src/DumpingTranslator.php b/src/DumpingTranslator.php index d11dfe5..efe2f1c 100644 --- a/src/DumpingTranslator.php +++ b/src/DumpingTranslator.php @@ -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; } @@ -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; + } } diff --git a/src/LaravelTranslationDumperServiceProvider.php b/src/LaravelTranslationDumperServiceProvider.php index 3703c22..ac65635 100644 --- a/src/LaravelTranslationDumperServiceProvider.php +++ b/src/LaravelTranslationDumperServiceProvider.php @@ -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'), ), ); } diff --git a/src/TranslationDumper.php b/src/TranslationDumper.php index 5004004..4742d46 100644 --- a/src/TranslationDumper.php +++ b/src/TranslationDumper.php @@ -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 {