Skip to content

Commit 2d34a18

Browse files
committed
chore: add a config file
1 parent cbffb05 commit 2d34a18

File tree

5 files changed

+69
-31
lines changed

5 files changed

+69
-31
lines changed

README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ composer require domthomas-dev/laravel-localized-enum
1212

1313
The package will automatically register its service provider.
1414

15+
## Configuration
16+
17+
Optionally, you can publish the configuration file:
18+
19+
```bash
20+
php artisan vendor:publish --tag=localized-enum-config
21+
```
22+
23+
This will create a `config/localized-enum.php` file where you can customize:
24+
25+
```php
26+
return [
27+
// Default label type when none is specified
28+
'default_label' => 'label',
29+
30+
// Translation file name (without .php extension)
31+
'translation_file' => 'enums',
32+
];
33+
```
34+
1535
## Usage
1636

1737
### Basic Usage
@@ -179,13 +199,15 @@ $options = ContactType::options(label: 'description', locale: 'fr');
179199
The package automatically generates translation keys using this format:
180200

181201
```
182-
enums.{EnumClass}.{label_type}.{case_name}
202+
{translation_file}.{EnumClass}.{label_type}.{case_name}
183203
```
184204

185-
For example:
205+
For example (with default config):
186206
- `App\Enums\ContactType::EMAIL``enums.App\Enums\ContactType.label.EMAIL`
187207
- With description: `enums.App\Enums\ContactType.description.EMAIL`
188208

209+
You can customize the translation file name and default label type in the configuration.
210+
189211
### Utility Methods
190212

191213
```php
@@ -211,7 +233,7 @@ echo ContactType::EMAIL->label(); // "EMAIL" (if no translation found)
211233

212234
### Translation Structure
213235

214-
The translation structure uses the enum class as the top-level key:
236+
The translation structure uses the enum class as the top-level key in your translation files (default: `resources/lang/{locale}/enums.php`):
215237

216238
```php
217239
use App\Enums\ContactType;
@@ -235,6 +257,9 @@ return [
235257
];
236258
```
237259

260+
**Note**: If you change the `translation_file` config to something other than 'enums', make sure to update your file names accordingly (e.g., `translations.php` if you set it to 'translations').
261+
```
262+
238263
## Testing
239264
240265
```bash

config/localized-enum.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Default Label Type
7+
|--------------------------------------------------------------------------
8+
|
9+
| This value determines the default label type used when no specific
10+
| label type is provided to the label() method.
11+
|
12+
*/
13+
'default_label' => 'label',
14+
15+
/*
16+
|--------------------------------------------------------------------------
17+
| Translation File Name
18+
|--------------------------------------------------------------------------
19+
|
20+
| This value determines the name of the translation file that contains
21+
| the enum translations. The file should be located in your lang directory.
22+
|
23+
*/
24+
'translation_file' => 'enums',
25+
];

src/LocalizedEnum.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ public function label(?string $label = null, ?string $locale = null): string
2828
*/
2929
protected function getLocalizationKey(?string $label = null): string
3030
{
31-
$labelType = $label ?: 'label';
32-
return 'enums.' . static::class . '.' . $labelType . '.' . $this->name;
31+
$label ??= config('localized-enum.default_label', 'label');
32+
$translationFile = config('localized-enum.translation_file', 'enums');
33+
34+
return $translationFile . '.' . static::class . '.' . $label . '.' . $this->name;
3335
}
3436

3537
/**

src/LocalizedEnumServiceProvider.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ class LocalizedEnumServiceProvider extends ServiceProvider
1111
*/
1212
public function boot(): void
1313
{
14-
//
14+
if ($this->app->runningInConsole()) {
15+
$this->publishes([
16+
__DIR__.'/../config/localized-enum.php' => config_path('localized-enum.php'),
17+
], 'localized-enum-config');
18+
}
1519
}
1620

1721
/**
1822
* Register any application services.
1923
*/
2024
public function register(): void
2125
{
22-
//
26+
$this->mergeConfigFrom(
27+
__DIR__.'/../config/localized-enum.php', 'localized-enum'
28+
);
2329
}
2430
}

tests/TestCase.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,11 @@ protected function getPackageProviders($app)
2121

2222
public function getEnvironmentSetUp($app)
2323
{
24+
// Set up config for testing
25+
$app['config']->set('localized-enum.default_label', 'label');
26+
$app['config']->set('localized-enum.translation_file', 'enums');
27+
2428
// Set up translations for testing
2529
$app['translator']->addNamespace('test', __DIR__ . '/lang');
26-
27-
// Set up test translations
28-
$app['translator']->setLoaded([
29-
'test::enums' => [
30-
'DomThomas\LocalizedEnum\Tests\Enums\TestEnum' => [
31-
'label' => [
32-
'FIRST' => 'First Option',
33-
'SECOND' => 'Second Option',
34-
'THIRD' => 'Third Option',
35-
],
36-
'description' => [
37-
'FIRST' => 'First option description',
38-
'SECOND' => 'Second option description',
39-
'THIRD' => 'Third option description',
40-
],
41-
],
42-
'DomThomas\LocalizedEnum\Tests\Enums\StatusEnum' => [
43-
'label' => [
44-
'ACTIVE' => 'Active Status',
45-
'INACTIVE' => 'Inactive Status',
46-
],
47-
],
48-
],
49-
]);
5030
}
5131
}

0 commit comments

Comments
 (0)