From 044937e1e134a8eb27d33d13bdbcbf4ba91cdc54 Mon Sep 17 00:00:00 2001 From: bambamboole Date: Fri, 23 Aug 2024 22:08:53 +0200 Subject: [PATCH] Do not dump non dotted translation keys by default --- config/config.php | 1 + src/DumpingTranslator.php | 5 +++++ src/LaravelTranslationDumperServiceProvider.php | 1 + tests/Unit/DumpingTranslatorTest.php | 2 +- 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/config/config.php b/config/config.php index 8ded3bb..7dd4234 100644 --- a/config/config.php +++ b/config/config.php @@ -4,6 +4,7 @@ 'dump_translations' => env('DUMP_TRANSLATIONS', false), 'dumper' => \Bambamboole\LaravelTranslationDumper\TranslationDumper::class, 'dump_prefix' => 'x-', + 'dump_non_dotted_keys' => false, 'ignore_keys' => [ // These keys are used by Laravel validator to check if a custom validation message is present 'validation.custom', diff --git a/src/DumpingTranslator.php b/src/DumpingTranslator.php index 1ce3d37..c8e4c15 100644 --- a/src/DumpingTranslator.php +++ b/src/DumpingTranslator.php @@ -16,6 +16,7 @@ public function __construct( private readonly TranslationDumperInterface $translationDumper, private readonly string $dumpPrefix = 'x-', private readonly array $ignoreKeys = [], + private readonly bool $dumpNonDottedKeys = false, ) {} public function get($key, array $replace = [], $locale = null, $fallback = true) @@ -61,6 +62,10 @@ public function __destruct() private function shouldBeIgnored(string $key): bool { + if (! $this->dumpNonDottedKeys && TranslationIdentifier::identify($key) === TranslationType::JSON) { + return true; + } + foreach ($this->ignoreKeys as $ignoreKey) { if (str_contains($key, $ignoreKey)) { return true; diff --git a/src/LaravelTranslationDumperServiceProvider.php b/src/LaravelTranslationDumperServiceProvider.php index d7e2394..9be86be 100644 --- a/src/LaravelTranslationDumperServiceProvider.php +++ b/src/LaravelTranslationDumperServiceProvider.php @@ -46,6 +46,7 @@ public function register(): void $app->make(TranslationDumperInterface::class), $app->make(Repository::class)->get('translation.dump_prefix'), $app->make(Repository::class)->get('translation.ignore_keys'), + $app->make(Repository::class)->get('translation.dump_non_dotted_keys'), ), ); } diff --git a/tests/Unit/DumpingTranslatorTest.php b/tests/Unit/DumpingTranslatorTest.php index bc30713..fcb87ce 100644 --- a/tests/Unit/DumpingTranslatorTest.php +++ b/tests/Unit/DumpingTranslatorTest.php @@ -13,7 +13,7 @@ class DumpingTranslatorTest extends TestCase { private const TEST_LOCALE = 'de'; - private const TEST_KEY = 'foo'; + private const TEST_KEY = 'foo.bar'; private const TEST_REPLACEMENTS = ['buzz' => 'light year'];