-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Laravel-Lang/1.x
Adding route groups with a locale prefix
- Loading branch information
Showing
17 changed files
with
203 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\Routes\Facades; | ||
|
||
use Closure; | ||
use Illuminate\Support\Facades\Facade; | ||
use LaravelLang\Routes\Services\Route; | ||
|
||
/** | ||
* @method static void group(Closure $callback) | ||
*/ | ||
class LocalizationRoute extends Facade | ||
{ | ||
protected static function getFacadeAccessor(): string | ||
{ | ||
return Route::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\Routes\Helpers; | ||
|
||
use LaravelLang\Config\Facades\Config; | ||
|
||
class Route | ||
{ | ||
public static function prefix(): string | ||
{ | ||
return Config::shared()->routes->namePrefix; | ||
} | ||
|
||
public static function redirect(): bool | ||
{ | ||
return Config::shared()->routes->redirect; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\Routes\Middlewares; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
use LaravelLang\Routes\Helpers\Route; | ||
|
||
class LocalizationByParameterPrefix extends LocalizationByParameterWithRedirect | ||
{ | ||
protected function parameters(Request $request): array | ||
{ | ||
return Arr::except($request->route()?->parameters() ?? [], [ | ||
$this->names()->parameter, | ||
]); | ||
} | ||
|
||
protected function routeName(Request $request): ?string | ||
{ | ||
if ($name = $request->route()?->getName()) { | ||
return Str::after($name, $this->routePrefix()); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected function routePrefix(): string | ||
{ | ||
return Route::prefix(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\Routes\Services; | ||
|
||
use Closure; | ||
use Illuminate\Support\Facades\Route as BaseRoute; | ||
use LaravelLang\Routes\Concerns\RouteParameters; | ||
use LaravelLang\Routes\Helpers\Route as RouteName; | ||
use LaravelLang\Routes\Middlewares\LocalizationByCookie; | ||
use LaravelLang\Routes\Middlewares\LocalizationByHeader; | ||
use LaravelLang\Routes\Middlewares\LocalizationByParameterPrefix; | ||
use LaravelLang\Routes\Middlewares\LocalizationBySession; | ||
|
||
class Route | ||
{ | ||
use RouteParameters; | ||
|
||
public function group(Closure $callback): void | ||
{ | ||
BaseRoute::middleware([ | ||
LocalizationByCookie::class, | ||
LocalizationByHeader::class, | ||
LocalizationBySession::class, | ||
])->group($callback); | ||
|
||
BaseRoute::prefix('{' . $this->names()->parameter . '}') | ||
->name(RouteName::prefix()) | ||
->middleware([ | ||
LocalizationByParameterPrefix::class, | ||
LocalizationByCookie::class, | ||
LocalizationByHeader::class, | ||
LocalizationBySession::class, | ||
])->group($callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use LaravelLang\Routes\Helpers\Route as RouteName; | ||
use Tests\Constants\LocaleValue; | ||
|
||
test('group', function () { | ||
$foo = 'test'; | ||
|
||
expect(route('via.group', compact('foo'))) | ||
->toBeString() | ||
->toBe('http://localhost/group/test'); | ||
|
||
expect(route(RouteName::prefix() . 'via.group', [ | ||
'locale' => LocaleValue::LocaleMain, | ||
'foo' => $foo, | ||
]))->toBeString()->toBe('http://localhost/fr/group/test'); | ||
}); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use LaravelLang\Routes\Helpers\Route as RouteName; | ||
use Tests\Constants\LocaleValue; | ||
|
||
use function Pest\Laravel\getJson; | ||
|
||
test('main without prefix', function () { | ||
$foo = 'test'; | ||
|
||
getJson(route('via.group', compact('foo'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationFrench); | ||
}); | ||
|
||
test('main locale', function (string $locale) { | ||
$foo = 'test'; | ||
|
||
getJson(route(RouteName::prefix() . 'via.group', compact('foo', 'locale'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationFrench); | ||
|
||
assertEventDispatched(); | ||
})->with('main-locales'); | ||
|
||
test('aliased locale', function (string $locale) { | ||
$foo = 'test'; | ||
|
||
getJson(route(RouteName::prefix() . 'via.group', compact('foo', 'locale'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationGerman); | ||
|
||
assertEventDispatched(); | ||
})->with('aliased-locales'); | ||
|
||
test('uninstalled locale', function (string $locale) { | ||
$foo = 'test'; | ||
|
||
getJson(route(RouteName::prefix() . 'via.group', compact('foo', 'locale'))) | ||
->assertRedirectToRoute('via.group', [ | ||
'foo' => $foo, | ||
]); | ||
|
||
assertEventNotDispatched(); | ||
})->with('uninstalled-locales'); | ||
|
||
test('unknown locale', function (int|string $locale) { | ||
$foo = 'test'; | ||
|
||
getJson(route(RouteName::prefix() . 'via.group', compact('foo', 'locale'))) | ||
->assertRedirectToRoute('via.group', [ | ||
'foo' => $foo, | ||
]); | ||
|
||
assertEventNotDispatched(); | ||
})->with('unknown-locales'); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.