-
-
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 #19 from Laravel-Lang/1.x
Added automatic forwarding of the localization parameter when calling group methods
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\Routes\Services; | ||
|
||
use Illuminate\Routing\UrlGenerator as BaseGenerator; | ||
use Illuminate\Support\Str; | ||
use LaravelLang\Config\Facades\Config; | ||
|
||
use function app; | ||
use function array_merge; | ||
use function is_null; | ||
|
||
class UrlGenerator extends BaseGenerator | ||
{ | ||
public function route($name, $parameters = [], $absolute = true): string | ||
{ | ||
if ($this->isLocalizedGroupRoute($name) && ! is_null($route = $this->routes->getByName($name))) { | ||
return $this->toRoute($route, $this->resolveLocalizedParameters($parameters), $absolute); | ||
} | ||
|
||
return parent::route($name, $parameters, $absolute); | ||
} | ||
|
||
protected function isLocalizedGroupRoute(string $name): bool | ||
{ | ||
return Str::startsWith($name, $this->localizedRoutePrefix()); | ||
} | ||
|
||
protected function resolveLocalizedParameters(array $parameters): array | ||
{ | ||
return array_merge([ | ||
$this->localizeRouteParameter() => app()->getLocale(), | ||
], $parameters); | ||
} | ||
|
||
protected function localizedRoutePrefix(): string | ||
{ | ||
return Config::shared()->routes->namePrefix; | ||
} | ||
|
||
protected function localizeRouteParameter(): string | ||
{ | ||
return Config::shared()->routes->names->parameter; | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use LaravelLang\Config\Facades\Config; | ||
use Tests\Constants\LocaleValue; | ||
|
||
test('localized route generation', function () { | ||
$name = Config::shared()->routes->namePrefix; | ||
$locale = LocaleValue::LocaleMain; | ||
$fallback = LocaleValue::LocaleAliasParent; | ||
|
||
expect(route($name . 'via.group.facade', ['foo' => 'bar'])) | ||
->toEndWith("localhost/$locale/group/facade/bar"); | ||
|
||
expect(route($name . 'via.group.macro', ['foo' => 'bar', 'locale' => $fallback])) | ||
->toEndWith("localhost/$fallback/group/macro/bar"); | ||
}); | ||
|
||
test('non-localized route generation', function () { | ||
expect(route('via.group.facade', ['foo' => 'bar'])) | ||
->toEndWith('localhost/group/facade/bar'); | ||
|
||
expect(route('via.group.macro', ['foo' => 'bar'])) | ||
->toEndWith('localhost/group/macro/bar'); | ||
}); |