From df4e159d3a371631d34f29c6d8b0a879293e4738 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Fri, 15 Nov 2024 14:09:32 +0400 Subject: [PATCH] feat(core): `\LastDragon_ru\LaraASP\Core\Helpers\Translator` will not change keys with namespace (`::`). --- packages/core/src/Helpers/Translator.php | 7 ++++++- packages/core/src/Helpers/TranslatorTest.php | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/core/src/Helpers/Translator.php b/packages/core/src/Helpers/Translator.php index 3cbd57e9..e755d170 100644 --- a/packages/core/src/Helpers/Translator.php +++ b/packages/core/src/Helpers/Translator.php @@ -9,6 +9,7 @@ use function array_splice; use function end; +use function str_contains; /** * Special wrapper around {@see TranslatorContract} to help translate package's messages. @@ -62,7 +63,11 @@ public function setLocale(string $locale): static { } protected function key(string $key): string { - return "{$this->getName()}::messages.{$key}"; + if (!str_contains($key, '::')) { + $key = "{$this->getName()}::messages.{$key}"; + } + + return $key; } /** diff --git a/packages/core/src/Helpers/TranslatorTest.php b/packages/core/src/Helpers/TranslatorTest.php index 421e7c42..191a64eb 100644 --- a/packages/core/src/Helpers/TranslatorTest.php +++ b/packages/core/src/Helpers/TranslatorTest.php @@ -201,6 +201,22 @@ static function (TestCase $test, string $currentLocale, string $fallbackLocale): ], 'unk', ], + 'translation exists (namespace)' => [ + 'translated text', + static function (TestCase $test, string $currentLocale, string $fallbackLocale): array { + return [ + $currentLocale => [ + 'test::should.be.translated' => '{1} one |[2,*] translated :value', + ], + ]; + }, + ['should.not.be.translated', 'test::should.be.translated', 'default'], + 2, + [ + 'value' => 'text', + ], + null, + ], ]; } //