You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -84,15 +85,53 @@ Setting this option to `'en'` will result, for example, in URL's like this:
84
85
85
86
> This option has no effect if you use domains instead of slugs.
86
87
87
-
#### Automatically Set Locale for Localized Routes
88
+
#### ☑️ Use Middleware to Update App Locale
89
+
90
+
By default, the app locale will always be what you configured in `config/app.php`. To automatically update the app locale when a localized route is accessed, you need to use a middleware.
91
+
92
+
**⚠️ Important note for Laravel 6+**
93
+
94
+
To make route model binding work in Laravel 6+ you always also need to add the middleware to the `$middlewarePriority` array in `app/Http/Kernel.php` so it runs before `SubstituteBindings`:
95
+
96
+
```php
97
+
protected $middlewarePriority = [
98
+
\Illuminate\Session\Middleware\StartSession::class, // <= after this
#### ☑️ Set Options for the Current Localized Route Group
118
157
119
-
To set an option for one localized route group only, you can specify it as the second parameter of the localized route macro:
158
+
To set an option for one localized route group only, you can specify it as the second parameter of the localized route macro. This will override the config file settings.
Because the former is rather ugly, this package overwrites the `route()` function and the underlying `UrlGenerator` class with an additional, optional `$locale` argument and takes care of the locale prefix for you. If you don't specify a locale, either a normal, non-localized route or a route in the current locale is returned.
194
233
234
+
```php
235
+
$url = route('admin.reports.index'); // current locale
Laravel's `Redirector` uses the same `UrlGenerator` as the `route()` function behind the scenes. Because we are overriding this class, you can easily redirect to your routes.
223
269
224
270
```php
225
-
return redirect()->route('home'); // redirects to /home
226
-
return redirect()->route('about'); // redirects to /en/about (current locale)
271
+
return redirect()->route('home'); // non-localized route, redirects to /home
272
+
return redirect()->route('about'); // localized route, redirects to /en/about (current locale)
227
273
```
228
274
229
275
You can't redirect to URL's in a specific locale this way, but if you need to, you can of course just use the `route()` function.
230
276
231
277
```php
232
-
return redirect(route('about', [], true, 'nl')); // redirects to /nl/about
> If a translation is not found, the original segment is used.
293
339
294
-
## 🚏 Route Placeholders
340
+
## 🚏 Route Parameters
295
341
296
-
Placeholders are not translated via language files. These are values you would provide via the `route()` function. The `Lang::uri()` macro will skip any placeholder segment.
342
+
Parameter placeholders are not translated via language files. These are values you would provide via the `route()` function. The `Lang::uri()` macro will skip any parameter placeholder segment.
297
343
298
344
If you have a model that uses a route key that is translated in the current locale, then you can still simply pass the model to the `route()` function to get translated URL's.
299
345
300
346
An example...
301
347
302
-
#### Given we have a model like this:
348
+
**Given we have a model like this:**
303
349
304
350
```php
305
351
class Post extends \Illuminate\Database\Eloquent\Model
@@ -318,7 +364,7 @@ class Post extends \Illuminate\Database\Eloquent\Model
318
364
319
365
> **TIP:** checkout [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable) for translatable models.
0 commit comments