Skip to content

Commit

Permalink
feat(Translations): Add getOptional method to allow return null if tr…
Browse files Browse the repository at this point in the history
…anslation is not set + support arrayable keys

BREAKING CHANGE: AbstractTranslations: $key parameter in methods get/getChoice/getArray/getKey accepts string|array instead of string
  • Loading branch information
pionl committed Jan 12, 2023
1 parent eabf005 commit e69f381
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Translations/AbstractProviderTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 18 additions & 5 deletions src/Translations/AbstractTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,22 +39,35 @@ 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
): string {
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);
}
}
33 changes: 33 additions & 0 deletions tests/Feature/Translations/AbstractTranslationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -51,4 +79,9 @@ public function testCustomDefaultValue(): void
{
$this->assertEquals('test123', $this->translations->getCustomNotFound());
}

public function testCustomDefaultNullValue(): void
{
$this->assertEquals(null, $this->translations->getCustomNotFoundNullable());
}
}
35 changes: 30 additions & 5 deletions tests/Feature/Translations/MyTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit e69f381

Please sign in to comment.