Skip to content

Commit

Permalink
feat: added translated method
Browse files Browse the repository at this point in the history
  • Loading branch information
sakanjo committed Feb 8, 2025
1 parent d8edad6 commit 3cab779
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Table of Contents
* [2. Create lang file](#2-create-lang-file)
* [Methods](#-methods)
* [getLabel](#getlabel)
* [translated](#translated)
* [is](#is)
* [isNot](#isnot)
* [tryFromName](#tryfromname)
Expand Down Expand Up @@ -98,6 +99,14 @@ Returns the label of the enum value.
Status::Active->getLabel(); // Active
```

### translated

Returns the translated label of the enum value.

```php
Status::Active->translated('tr'); // Aktif
```

### is

Checks if the enum is equal to another one.
Expand Down
12 changes: 6 additions & 6 deletions src/EasyEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ trait EasyEnum
{
// --- Translatable ---

private function getNamespace(): string
public function getTranslationNamespace(): string
{
$class = static::class;

return "enums.$class.$this->name";
}

private function defaultLabel(): string
public function getDefaultLabel(): string
{
return Str::of($this->name)
->headline()
->lower()
->ucfirst();
}

private function translated(): string
public function translated(?string $locale = null): string
{
return Lang::get($this->getNamespace());
return Lang::get($this->getTranslationNamespace(), locale: $locale);
}

public function getLabel(): string
{
return Lang::has($this->getNamespace())
return Lang::has($this->getTranslationNamespace())
? $this->translated()
: $this->defaultLabel();
: $this->getDefaultLabel();
}

// --- Equality ---
Expand Down
10 changes: 10 additions & 0 deletions tests/src/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
[ExampleEnum::NOPE, 'N o p e'],
]);

it('returns correct translation using `translated`', function (ExampleEnum $enum, ?string $locale, string $expected) {
assertEquals($enum->translated($locale), $expected);
})
->with([
[ExampleEnum::USD, 'en', 'USD'],
[ExampleEnum::EURO, null, 'EURO'],
[ExampleEnum::USD, 'tr', 'Dolar'],
[ExampleEnum::NOPE, null, 'enums.SaKanjo\EasyEnum\Tests\Enums\ExampleEnum.NOPE'],
]);

it('compares correctly using `is`', function (ExampleEnum $a, ExampleEnum $b, bool $expected) {
assertEquals($a->is($b), $expected);
})
Expand Down
9 changes: 9 additions & 0 deletions tests/src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ protected function defineEnvironment($app): void

private function loadLangFiles(): void
{
// EN
$enums = require __DIR__.'/lang/en/enums.php';
$lines = Arr::mapWithKeys($enums, fn ($value, $key) => [
'enums.'.$key => $value,
]);

Lang::addLines($lines, 'en');

// TR
$enums = require __DIR__.'/lang/tr/enums.php';
$lines = Arr::mapWithKeys($enums, fn ($value, $key) => [
'enums.'.$key => $value,
]);

Lang::addLines($lines, 'tr');
}
}
9 changes: 9 additions & 0 deletions tests/src/lang/tr/enums.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use SaKanjo\EasyEnum\Tests\Enums;

return [
Enums\ExampleEnum::class => [
Enums\ExampleEnum::USD->name => 'Dolar',
],
];

0 comments on commit 3cab779

Please sign in to comment.