Skip to content

Commit 87eba11

Browse files
committed
Use a dedicated config file for supported locales
1 parent 0b418db commit 87eba11

File tree

7 files changed

+57
-11
lines changed

7 files changed

+57
-11
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ composer require codezero/laravel-localized-routes
1919

2020
> Laravel >= 5.5 will automatically register the ServiceProvider.
2121
22-
## Configure Supported Locales
22+
#### Publish Configuration File
2323

24-
Add a `locales` key to your `config/app.php` file.
24+
```php
25+
php artisan vendor:publish --provider="CodeZero\LocalizedRoutes\LocalizedRoutesServiceProvider" --tag="config"
26+
```
27+
28+
You will now find a `localize-routes.php` file in the `config` folder.
29+
30+
#### Configure Supported Locales
31+
32+
Add any locales you wish to support to your published `config/localized-routes.php` file:
2533

2634
```php
27-
'locales' => [
28-
'en',
29-
'nl',
30-
//...
31-
],
35+
'supported-locales' => ['en', 'nl', 'fr'],
3236
```
3337

3438
## Register Routes

config/localized-routes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return [
4+
5+
/**
6+
* The locales you wish to support.
7+
*/
8+
'locales' => [],
9+
10+
];

src/LocalizedRoutesServiceProvider.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@
88

99
class LocalizedRoutesServiceProvider extends ServiceProvider
1010
{
11+
/**
12+
* The package name.
13+
*
14+
* @var string
15+
*/
16+
protected $name = 'localized-routes';
17+
1118
/**
1219
* Bootstrap any application services.
1320
*
1421
* @return void
1522
*/
1623
public function boot()
1724
{
25+
$this->registerPublishableFiles();
1826
$this->registerMacros();
1927
}
2028

@@ -25,6 +33,7 @@ public function boot()
2533
*/
2634
public function register()
2735
{
36+
$this->mergeConfig();
2837
$this->registerUrlGenerator();
2938
}
3039

@@ -39,6 +48,29 @@ protected function registerMacros()
3948
UriTranslationMacro::register();
4049
}
4150

51+
/**
52+
* Register the publishable files.
53+
*
54+
* @return void
55+
*/
56+
protected function registerPublishableFiles()
57+
{
58+
$this->publishes([
59+
__DIR__."/../config/{$this->name}.php" => config_path("{$this->name}.php"),
60+
], 'config');
61+
}
62+
63+
/**
64+
* Merge published configuration file with
65+
* the original package configuration file.
66+
*
67+
* @return void
68+
*/
69+
protected function mergeConfig()
70+
{
71+
$this->mergeConfigFrom(__DIR__."/../config/{$this->name}.php", $this->name);
72+
}
73+
4274
/**
4375
* Register the URL generator service.
4476
*

src/Macros/LocalizedRoutesMacro.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function register()
2020
// change it during route registration.
2121
$currentLocale = App::getLocale();
2222

23-
$locales = Config::get('app.locales', []);
23+
$locales = Config::get('localized-routes.supported-locales', []);
2424

2525
foreach ($locales as $locale) {
2626
// Change the current locale so we can

src/UrlGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected function stripLocaleFromRouteName($name)
8585
return $name;
8686
}
8787

88-
$locales = Config::get('app.locales', []);
88+
$locales = Config::get('localized-routes.supported-locales', []);
8989

9090
// If the first part of the route name is a valid
9191
// locale, then remove it from the array.

tests/Unit/Macros/LocalizedRoutesMacroTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class LocalizedRoutesMacroTest extends TestCase
99
{
1010
protected function setAvailableLocales($locales)
1111
{
12-
config()->set('app.locales', $locales);
12+
config()->set('localized-routes.supported-locales', $locales);
1313
}
1414

1515
/** @test */

tests/Unit/UrlGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected function setUp()
1414
{
1515
parent::setUp();
1616

17-
config()->set('app.locales', ['en', 'nl']);
17+
config()->set('localized-routes.supported-locales', ['en', 'nl']);
1818

1919
app()->setLocale('en');
2020
}

0 commit comments

Comments
 (0)