Skip to content

Commit bd128b9

Browse files
authored
Add RouteActionDetector (#11)
1 parent d65f639 commit bd128b9

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,22 @@ Add any locales you wish to support to your published `config/localizer.php` fil
8787

8888
By default, the middleware will use the following detectors to check for a supported locale in:
8989

90-
1. The URL slug
91-
2. A main omitted locale
92-
3. The authenticated user model
93-
4. The session
94-
5. A cookie
95-
6. The browser
96-
7. The app's default locale
90+
1. A custom route action
91+
2. The URL slug
92+
3. A main omitted locale
93+
4. The authenticated user model
94+
5. The session
95+
6. A cookie
96+
7. The browser
97+
8. The app's default locale
98+
99+
You can set a custom attribute or "action" on a route group.
100+
This is especially useful in combination with the [codezero/laravel-localized-routes](https://github.com/codezero-be/laravel-localized-routes) package.
97101

98102
If you set an omitted locale, no additional detectors will run after the `OmittedLocaleDetector`.
99103
This makes sense, because the locale will always be determined by the URL in this scenario.
100104

101-
You can configure the session key, cookie name and the attribute on the user model that holds the locale.
105+
You can configure the route action, session key, cookie name and the attribute on the user model that holds the locale.
102106
By default this is all set to `locale`. If the user model does not have this attribute, it will skip this check.
103107

104108
You can also choose which detectors to run and in what order.

config/localizer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* These will be executed in the order that they are added to the array!
2121
*/
2222
'detectors' => [
23+
CodeZero\Localizer\Detectors\RouteActionDetector::class,
2324
CodeZero\Localizer\Detectors\UrlDetector::class,
2425
CodeZero\Localizer\Detectors\OmittedLocaleDetector::class,
2526
CodeZero\Localizer\Detectors\UserDetector::class,
@@ -44,6 +45,12 @@
4445
*/
4546
'url-segment' => 1,
4647

48+
/**
49+
* The attribute or "action" on the route that holds the locale,
50+
* when using the RouteActionDetector.
51+
*/
52+
'route-action' => 'locale',
53+
4754
/**
4855
* The attribute on the user model that holds the locale,
4956
* when using the UserDetector.

src/Detectors/RouteActionDetector.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace CodeZero\Localizer\Detectors;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Config;
7+
8+
class RouteActionDetector implements Detector
9+
{
10+
/**
11+
* The current request.
12+
*
13+
* @var \Illuminate\Http\Request
14+
*/
15+
protected $request;
16+
17+
/**
18+
* Create a new Detector instance.
19+
*
20+
* @param \Illuminate\Http\Request $request
21+
*/
22+
public function __construct(Request $request)
23+
{
24+
$this->request = $request;
25+
}
26+
27+
/**
28+
* Detect the locale.
29+
*
30+
* @return string|array|null
31+
*/
32+
public function detect()
33+
{
34+
$action = Config::get('localizer.route-action');
35+
36+
return $this->request->route()->getAction($action);
37+
}
38+
}

tests/Feature/SetLocaleTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,31 @@ protected function setUp(): void
3232
}
3333

3434
/** @test */
35-
public function it_looks_for_a_locale_in_the_url_first()
35+
public function it_looks_for_a_locale_in_a_custom_route_action()
36+
{
37+
$this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']);
38+
$this->setSessionLocale('fr');
39+
$this->setBrowserLocales('it');
40+
$this->setAppLocale('en');
41+
$cookie = 'de';
42+
43+
Route::group([
44+
'locale' => 'nl',
45+
], function () {
46+
Route::get('some/route', function () {
47+
return App::getLocale();
48+
})->middleware(['web', SetLocale::class]);
49+
});
50+
51+
$response = $this->getWithCookie('some/route', $cookie);
52+
53+
$response->assertSessionHas($this->sessionKey, 'nl');
54+
$response->assertCookie($this->cookieName, 'nl');
55+
$this->assertEquals('nl', $response->original);
56+
}
57+
58+
/** @test */
59+
public function it_looks_for_a_locale_in_the_url()
3660
{
3761
$this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']);
3862
$this->setSessionLocale('fr');

0 commit comments

Comments
 (0)