From 644c389e2b67b906bd1ccd12ab95a975069eacd2 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 10 Jul 2024 13:27:35 +0300 Subject: [PATCH] Added `localizedRoute` function --- composer.json | 5 +++- helpers/functions.php | 25 +++++++++++++++++++ ide.json | 18 ++++++++++++++ tests/Unit/Helpers/LocalizedRouteTest.php | 29 +++++++++++++++++++++++ tests/Unit/Services/UrlGeneratorTest.php | 26 -------------------- 5 files changed, 76 insertions(+), 27 deletions(-) create mode 100644 helpers/functions.php create mode 100644 ide.json create mode 100644 tests/Unit/Helpers/LocalizedRouteTest.php delete mode 100644 tests/Unit/Services/UrlGeneratorTest.php diff --git a/composer.json b/composer.json index 80a1fba..e425ae8 100644 --- a/composer.json +++ b/composer.json @@ -47,7 +47,10 @@ "autoload": { "psr-4": { "LaravelLang\\Routes\\": "src/" - } + }, + "files": [ + "helpers/functions.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/helpers/functions.php b/helpers/functions.php new file mode 100644 index 0000000..5bd7ec1 --- /dev/null +++ b/helpers/functions.php @@ -0,0 +1,25 @@ +routes->names->parameter; + $prefix = Config::shared()->routes->namePrefix; + + $name = Str::start($route, $prefix); + + if (! Route::has($name)) { + return route($route, $parameters, $absolute); + } + + return route($name, array_merge([ + $locale => app()->getLocale(), + ], $parameters), $absolute); + } +} diff --git a/ide.json b/ide.json new file mode 100644 index 0000000..a1928f6 --- /dev/null +++ b/ide.json @@ -0,0 +1,18 @@ +{ + "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json", + "completions": [ + { + "complete": "routeName", + "condition": [ + { + "functionNames": [ + "localizedRoute" + ], + "parameters": [ + 1 + ] + } + ] + } + ] +} diff --git a/tests/Unit/Helpers/LocalizedRouteTest.php b/tests/Unit/Helpers/LocalizedRouteTest.php new file mode 100644 index 0000000..753d04a --- /dev/null +++ b/tests/Unit/Helpers/LocalizedRouteTest.php @@ -0,0 +1,29 @@ +routes->namePrefix; + $locale = LocaleValue::LocaleMain; + $fallback = LocaleValue::LocaleAliasParent; + + expect(localizedRoute($name . 'via.group.facade', ['foo' => 'bar'])) + ->toEndWith("localhost/$locale/group/facade/bar"); + + expect(localizedRoute($name . 'via.group.macro', ['foo' => 'bar', 'locale' => $fallback])) + ->toEndWith("localhost/$fallback/group/macro/bar"); + + expect(localizedRoute('via.group.facade', ['foo' => 'bar'])) + ->toEndWith("localhost/$locale/group/facade/bar"); + + expect(localizedRoute('via.group.macro', ['foo' => 'bar', 'locale' => $fallback])) + ->toEndWith("localhost/$fallback/group/macro/bar"); +}); + +test('routes without groups', function () { + expect(localizedRoute('via.model.default', ['foo' => 'bar'])) + ->toEndWith('localhost/model/default/bar'); +}); diff --git a/tests/Unit/Services/UrlGeneratorTest.php b/tests/Unit/Services/UrlGeneratorTest.php deleted file mode 100644 index 22930ba..0000000 --- a/tests/Unit/Services/UrlGeneratorTest.php +++ /dev/null @@ -1,26 +0,0 @@ -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'); -});