Skip to content

Commit d9265ff

Browse files
Merge pull request #27 from bald-cat/patch-1
Adding default locale hiding during URL generation
2 parents 7d6aea3 + 22f7c37 commit d9265ff

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

helpers/functions.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@
55
use Illuminate\Support\Facades\Route;
66
use Illuminate\Support\Str;
77
use LaravelLang\Config\Facades\Config;
8+
use LaravelLang\Locales\Facades\Locales;
9+
use LaravelLang\Routes\Helpers\Route as RouteHelper;
810

911
if (! function_exists('localizedRoute')) {
1012
function localizedRoute(string $route, array $parameters = [], bool $absolute = true): string
1113
{
1214
$locale = Config::shared()->routes->names->parameter;
1315
$prefix = Config::shared()->routes->namePrefix;
1416

15-
$name = Str::start($route, $prefix);
17+
$parameters[$locale] ??= Locales::raw()->getFallback();
1618

19+
if (RouteHelper::hidingFallback($parameters[$locale])) {
20+
unset($parameters[$locale]);
21+
22+
return route($route, $parameters, $absolute);
23+
}
24+
25+
$name = Str::start($route, $prefix);
1726
$route = Route::has($name) ? $name : $route;
1827

1928
return route($route, array_merge([

src/Helpers/Route.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace LaravelLang\Routes\Helpers;
66

77
use LaravelLang\Config\Facades\Config;
8+
use LaravelLang\Locales\Facades\Locales;
9+
use LaravelLang\LocaleList\Locale;
810

911
class Route
1012
{
@@ -17,4 +19,18 @@ public static function redirect(): bool
1719
{
1820
return Config::shared()->routes->redirect;
1921
}
22+
23+
public static function hide(): bool
24+
{
25+
return Config::shared()->routes->hide;
26+
}
27+
28+
public static function hidingFallback(Locale|string $locale): bool
29+
{
30+
if (! static::hide() || ! Locales::isInstalled($locale)) {
31+
return false;
32+
}
33+
34+
return Locales::raw()->get($locale) === Locales::raw()->getFallback();
35+
}
2036
}

tests/Unit/Helpers/LocalizedRouteTest.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use LaravelLang\Config\Enums\Name;
56
use LaravelLang\Config\Facades\Config;
67
use Tests\Constants\LocaleValue;
78

@@ -10,13 +11,13 @@
1011
$locale = LocaleValue::LocaleMain;
1112
$fallback = LocaleValue::LocaleAliasParent;
1213

13-
expect(localizedRoute($name . 'via.group.facade', ['foo' => 'bar']))
14+
expect(localizedRoute($name . 'via.group.facade', ['foo' => 'bar', 'locale' => $locale]))
1415
->toEndWith("localhost/$locale/group/facade/bar");
1516

1617
expect(localizedRoute($name . 'via.group.macro', ['foo' => 'bar', 'locale' => $fallback]))
1718
->toEndWith("localhost/$fallback/group/macro/bar");
1819

19-
expect(localizedRoute('via.group.facade', ['foo' => 'bar']))
20+
expect(localizedRoute('via.group.facade', ['foo' => 'bar', 'locale' => $locale]))
2021
->toEndWith("localhost/$locale/group/facade/bar");
2122

2223
expect(localizedRoute('via.group.macro', ['foo' => 'bar', 'locale' => $fallback]))
@@ -26,6 +27,45 @@
2627
test('routes without groups', function () {
2728
$locale = LocaleValue::LocaleMain;
2829

29-
expect(localizedRoute('via.model.default', ['foo' => 'bar']))
30+
expect(localizedRoute('via.model.default', ['foo' => 'bar', 'locale' => $locale]))
3031
->toEndWith('localhost/model/default/bar?locale=' . $locale);
3132
});
33+
34+
test('routes hide fallback', function (bool $set) {
35+
$locale = LocaleValue::LocaleMain;
36+
$fallback = LocaleValue::LocaleMain;
37+
38+
config()->set(Name::Shared() . '.routes.hide_default', true);
39+
config()->set('app.fallback_locale', $fallback);
40+
41+
$params = $set
42+
? ['foo' => 'bar', 'locale' => $locale]
43+
: ['foo' => 'bar'];
44+
45+
expect(localizedRoute('via.parameter', $params))
46+
->toEndWith("localhost/path/bar");
47+
})->with([true, false]);
48+
49+
test('routes hide manual', function () {
50+
$locale = LocaleValue::LocaleMain;
51+
$fallback = LocaleValue::LocaleAliasParent;
52+
53+
config()->set(Name::Shared() . '.routes.hide_default', true);
54+
config()->set('app.fallback_locale', $fallback);
55+
56+
expect(localizedRoute('via.parameter', ['foo' => 'bar', 'locale' => $locale]))
57+
->toEndWith("localhost/path/bar/$locale");
58+
59+
expect(localizedRoute('via.parameter', ['foo' => 'bar', 'locale' => $fallback]))
60+
->toEndWith("localhost/path/bar");
61+
});
62+
63+
test('routes does not hide', function () {
64+
$fallback = LocaleValue::LocaleAliasParent;
65+
66+
config()->set(Name::Shared() . '.routes.hide_default', true);
67+
config()->set('app.fallback_locale', $fallback);
68+
69+
expect(localizedRoute('via.parameter', ['foo' => 'bar']))
70+
->toEndWith("localhost/path/bar");
71+
});

0 commit comments

Comments
 (0)