File tree Expand file tree Collapse file tree 3 files changed +45
-3
lines changed Expand file tree Collapse file tree 3 files changed +45
-3
lines changed Original file line number Diff line number Diff line change 7
7
*/
8
8
'supported-locales ' => [],
9
9
10
+ /**
11
+ * If you have a main locale and don't want
12
+ * to prefix it in the URL, specify it here.
13
+ *
14
+ * 'omit_url_prefix_for_locale' => 'en',
15
+ */
16
+ 'omit_url_prefix_for_locale ' => null ,
17
+
10
18
];
Original file line number Diff line number Diff line change @@ -21,16 +21,25 @@ public static function register()
21
21
$ currentLocale = App::getLocale ();
22
22
23
23
$ locales = Config::get ('localized-routes.supported-locales ' , []);
24
+ $ omitPrefix = Config::get ('localized-routes.omit_url_prefix_for_locale ' );
24
25
25
26
foreach ($ locales as $ locale ) {
26
27
// Change the current locale so we can
27
28
// use it in the callback, for example
28
29
// to register translated route URI's.
29
30
App::setLocale ($ locale );
30
31
31
- // Wrap the localized routes in a group and prepend
32
- // the locale to the URI and the route name.
33
- Route::prefix ($ locale )->name ("{$ locale }. " )->group ($ callback );
32
+ // Create a new route and prepend
33
+ // the locale to the route name.
34
+ $ route = Route::name ("{$ locale }. " );
35
+
36
+ // Prefix the URL unless the locale
37
+ // is configured to be omitted.
38
+ if ($ locale !== $ omitPrefix ) {
39
+ $ route ->prefix ($ locale );
40
+ }
41
+
42
+ $ route ->group ($ callback );
34
43
}
35
44
36
45
// Restore the original locale.
Original file line number Diff line number Diff line change @@ -69,6 +69,31 @@ public function it_registers_a_root_route_for_each_locale()
69
69
$ this ->assertContains ('nl ' , $ uris );
70
70
}
71
71
72
+ /** @test */
73
+ public function it_registers_a_url_without_prefix_for_a_configured_main_locale ()
74
+ {
75
+ $ this ->setAvailableLocales (['en ' , 'nl ' ]);
76
+
77
+ Config::set ('localized-routes.omit_url_prefix_for_locale ' , 'en ' );
78
+
79
+ Route::localized (function () {
80
+ Route::get ('about ' , function () {})
81
+ ->name ('about ' );
82
+ });
83
+
84
+ $ routes = $ this ->getRoutes ();
85
+ $ names = $ routes ->pluck ('action.as ' );
86
+ $ uris = $ routes ->pluck ('uri ' );
87
+
88
+ $ this ->assertNotContains ('about ' , $ names );
89
+ $ this ->assertContains ('en.about ' , $ names );
90
+ $ this ->assertContains ('nl.about ' , $ names );
91
+
92
+ $ this ->assertNotContains ('en/about ' , $ uris );
93
+ $ this ->assertContains ('about ' , $ uris );
94
+ $ this ->assertContains ('nl/about ' , $ uris );
95
+ }
96
+
72
97
/** @test */
73
98
public function it_temporarily_changes_the_app_locale_when_registering_the_routes ()
74
99
{
You can’t perform that action at this time.
0 commit comments