diff --git a/src/Translations/AbstractProviderTranslations.php b/src/Translations/AbstractProviderTranslations.php index 8c6406f1..4d440578 100644 --- a/src/Translations/AbstractProviderTranslations.php +++ b/src/Translations/AbstractProviderTranslations.php @@ -44,7 +44,7 @@ public function __construct(Translator $translator, Application $application) */ abstract protected function getProviderClass(): string; - protected function getKey(string $key): string + protected function getKey(string|array $key): string { return $this->providerKey . '::' . parent::getKey($key); } diff --git a/src/Translations/AbstractTranslations.php b/src/Translations/AbstractTranslations.php index a70270da..c521ba2a 100644 --- a/src/Translations/AbstractTranslations.php +++ b/src/Translations/AbstractTranslations.php @@ -25,7 +25,7 @@ public function __construct( abstract public function getLocalizationKey(): string; protected function get( - string $key, + string|array $key, array $replace = [], ?string $locale = null, string $defaultValue = null @@ -39,13 +39,24 @@ protected function get( return $result; } - protected function getArray(string $key, array $replace = [], ?string $locale = null): array + protected function getOptional(string|array $key, array $replace = [], ?string $locale = null): ?string + { + $result = $this->translator->get($this->getKey($key), $replace, $locale); + + if ($result === $this->getKey($key)) { + return null; + } + + return $result; + } + + protected function getArray(string|array $key, array $replace = [], ?string $locale = null): array { return $this->translator->get($this->getKey($key), $replace, $locale); } protected function getChoice( - string $key, + string|array $key, int|array|Countable $number, array $replace = [], ?string $locale = null @@ -53,8 +64,10 @@ protected function getChoice( return $this->translator->choice($this->getKey($key), $number, $replace, $locale); } - protected function getKey(string $key): string + protected function getKey(string|array $key): string { - return $this->getLocalizationKey() . '.' . $key; + $keys = [$this->getLocalizationKey(), ...(is_array($key) ? $key : [$key])]; + + return implode('.', $keys); } } diff --git a/tests/Feature/Translations/AbstractTranslationsTest.php b/tests/Feature/Translations/AbstractTranslationsTest.php index 731b55d6..14810095 100644 --- a/tests/Feature/Translations/AbstractTranslationsTest.php +++ b/tests/Feature/Translations/AbstractTranslationsTest.php @@ -42,6 +42,34 @@ public function testArray(): void $this->assertEquals($expected, $this->translations->getWays()); } + public function testFromArray(): void + { + $expected = [ + 'one' => 'One way', + 'two' => 'Two way', + ]; + $this->assertEquals($expected, $this->translations->getWays()); + } + + public function testGetWay(): void + { + $this->assertEquals('One way', $this->translations->getWay('one')); + $this->assertEquals('Two way', $this->translations->getWay('two')); + } + + public function testGetWayWithArrayKeys(): void + { + $this->assertEquals('One way', $this->translations->getWayArrayKeys('one')); + $this->assertEquals('Two way', $this->translations->getWayArrayKeys('two')); + } + + public function testGetWayNullable(): void + { + $this->assertEquals('One way', $this->translations->getWayNullable('one')); + $this->assertEquals('Two way', $this->translations->getWayNullable('two')); + $this->assertEquals(null, $this->translations->getWayNullable('s')); + } + public function testDefaultValueByLaravel(): void { $this->assertEquals('package::test.test', $this->translations->getNotFoundLaravel()); @@ -51,4 +79,9 @@ public function testCustomDefaultValue(): void { $this->assertEquals('test123', $this->translations->getCustomNotFound()); } + + public function testCustomDefaultNullValue(): void + { + $this->assertEquals(null, $this->translations->getCustomNotFoundNullable()); + } } diff --git a/tests/Feature/Translations/MyTranslations.php b/tests/Feature/Translations/MyTranslations.php index 5ca92678..4168300f 100644 --- a/tests/Feature/Translations/MyTranslations.php +++ b/tests/Feature/Translations/MyTranslations.php @@ -15,28 +15,53 @@ public function getLocalizationKey(): string public function getMyTest(): string { - return $this->get('name'); + return $this->get(key: 'name'); } public function getAgo(int $number): string { - return $this->getChoice('minutes_ago', $number, [ + return $this->getChoice(key: 'minutes_ago', number: $number, replace: [ 'value' => $number, ]); } public function getWays(): array { - return $this->getArray('ways'); + return $this->getArray(key: 'ways'); + } + + public function getWay(string $key): string + { + return $this->get(key: ['ways', $key]); + } + + public function getWayArrayKeys(string $key): string + { + return $this->get(key: ['ways', $key]); + } + + public function getWayDotNotation(string $key): string + { + return $this->get(key: 'ways.' . $key); + } + + public function getWayNullable(string $key): ?string + { + return $this->getOptional(key: ['ways', $key]); } public function getNotFoundLaravel(): string { - return $this->get('test'); + return $this->get(key: 'test'); } public function getCustomNotFound(): string { - return $this->get('test', defaultValue: 'test123'); + return $this->get(key: 'test', defaultValue: 'test123'); + } + + public function getCustomNotFoundNullable(): ?string + { + return $this->getOptional(key: 'test'); } }