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
#### A convenient way to set up, manage and use localized routes in a Laravel app.
12
+
#### A convenient way to set up and use localized routes in a Laravel app.
13
13
14
-
-[Automatically register](#-register-routes) a route for each locale you wish to support.
15
-
- Use [URL slugs or custom domains](#-supported-locales) (or subdomains).
16
-
- Optionally omit the locale slug from the URL for your main locale.
14
+
## 🧩 Features
15
+
16
+
-[Automatically register](#-register-routes) a route for each locale.
17
+
- Use [URL slugs or custom domains](#%EF%B8%8F-supported-locales) (or subdomains).
18
+
- Optionally [omit the locale slug from the URL for your main locale](#%EF%B8%8F-omit-slug-for-main-locale).
19
+
- Optionally [translate each segment](#-translate-routes) in your URI's.
17
20
-[Generate localized route URL's](#-generate-route-urls) using the `route()` helper.
18
21
-[Redirect to localized routes](#-redirect-to-routes) using the `redirect()->route()` helper.
19
-
-[Generate localized signed route URL's](#-generate-signed-route-urls)
22
+
-[Generate localized signed route URL's](#-generate-signed-route-urls).
23
+
-[Automatically set the appropriate app locale](#%EF%B8%8F-use-middleware-to-update-app-locale) using the middleware.
24
+
- Optionally [detect and set the preferred locale in multiple sources](#%EF%B8%8F-use-localizer-to-detect-and-set-the-locale).
25
+
- Use localized route keys with [route model binding](#-route-model-binding).
20
26
- Allow routes to be [cached](#-cache-routes).
21
-
- Optionally [translate each segment](#-translate-routes) in your URI's.
22
-
- Use the middleware to [automatically set the appropriate app locale](#-use-middleware-to-update-app-locale) (and use [route model binding](#-route-model-binding)).
23
-
-**Work with routes without thinking too much about locales.**
@@ -62,11 +65,11 @@ Add any locales you wish to support to your published `config/localized-routes.p
62
65
'supported-locales' => ['en', 'nl', 'fr'],
63
66
```
64
67
65
-
This will automically prepend a slug to your localized routes. [More on this below](#-register-routes).
68
+
This will automatically prepend a slug to your localized routes. [More on this below](#-register-routes).
66
69
67
70
##### Using Domains
68
71
69
-
Alternatively, you can use a different domain or subdomain for each locale by adding them to the `supported-locales` like this:
72
+
Alternatively, you can use a different domain or subdomain for each locale by configuring the `supported-locales` like this:
70
73
71
74
```php
72
75
'supported-locales' => [
@@ -94,32 +97,36 @@ Setting this option to `'en'` will result, for example, in URL's like this:
94
97
95
98
#### ☑️ Use Middleware to Update App Locale
96
99
97
-
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.
100
+
By default, the app locale will always be what you configured in `config/app.php`.
101
+
To automatically update the app locale when a localized route is accessed, you need to use a middleware.
98
102
99
103
**⚠️ Important note for Laravel 6+**
100
104
101
-
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`:
105
+
To make route model binding work in Laravel 6+ you always also need to add the middleware
106
+
to the `$middlewarePriority` array in `app/Http/Kernel.php` so it runs before `SubstituteBindings`:
102
107
103
108
```php
104
109
protected $middlewarePriority = [
105
110
\Illuminate\Session\Middleware\StartSession::class, // <= after this
#### ☑️ Use Localizer to Detect and Set the Locale
171
+
172
+
This package can use [codezero/laravel-localizer](https://github.com/codezero-be/laravel-localizer) to automatically detect and set the locale.
173
+
174
+
With this option disabled, the app locale will only be updated when accessing localized routes.
175
+
176
+
With this option enabled, the app locale can also be updated when accessing non-localized routes.
177
+
For non-localized routes it will look for a preferred locale in the session, in a cookie or in the browser.
178
+
Additionally, it will also store the app locale in the session and in a cookie.
179
+
180
+
> Enabling this option can be handy if you have, for example, a generic homepage and you want to know the preferred locale.
181
+
182
+
To enable this option, set it to `true` in the published config file.
183
+
184
+
```php
185
+
'use_localizer' => true
186
+
```
187
+
188
+
This option only has effect on routes that use our `SetLocale` middleware.
189
+
190
+
> You can review [codezero/laravel-localizer](https://github.com/codezero-be/laravel-localizer),
191
+
> publish its config file and tweak it as needed.
192
+
> The only option we will override is `supported-locales`, to match the option in our own config file.
193
+
163
194
#### ☑️ Set Options for the Current Localized Route Group
164
195
165
-
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.
196
+
To set an option for one localized route group only, you can specify it as the second parameter of the localized route macro.
In the above example there are 5 routes being registered. The routes defined in the `Route::localized` closure are automatically registered for each configured locale. This will prepend the locale to the route's URI and name. If you configured custom domains, it will use those instead of the slugs.
235
+
In the above example there are 5 routes being registered.
236
+
The routes defined in the `Route::localized` closure are automatically registered for each configured locale.
237
+
This will prepend the locale to the route's URI and name. If you configured custom domains, it will use those instead of the slugs.
204
238
205
239
| URI | Name |
206
240
| ----------------- | ---------------------- |
@@ -220,7 +254,9 @@ If you set `omit_url_prefix_for_locale` to `'en'` in the configuration file, the
220
254
| /admin/reports | en.admin.reports.index |
221
255
| /nl/admin/reports | nl.admin.reports.index |
222
256
223
-
**⚠️ Beware that you don't register the same URL twice when omitting the locale.** You can't have a localized `/about` route and also register a non-localized `/about` route in this case. The same idea applies to the `/` (root) route! Also note that the route names still have the locale prefix.
257
+
**⚠️ Beware that you don't register the same URL twice when omitting the locale.**
258
+
You can't have a localized `/about` route and also register a non-localized `/about` route in this case.
259
+
The same idea applies to the `/` (root) route! Also note that the route names still have the locale prefix.
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.
275
+
Because the former is rather ugly, this package overwrites the `route()` function
276
+
and the underlying `UrlGenerator` class with an additional, optional `$locale` argument
277
+
and takes care of the locale prefix for you. If you don't specify a locale, either a normal,
278
+
non-localized route or a route in the current locale is returned.
240
279
241
280
```php
242
281
$url = route('admin.reports.index'); // current locale
> **Note:** in a most practical scenario you would register a route either localized **or** non-localized, but not both. If you do, you will always need to specify a locale to get the URL, because non-localized routes always have priority when using the `route()` function.
310
+
> **Note:** in a most practical scenario you would register a route either localized **or** non-localized, but not both.
311
+
> If you do, you will always need to specify a locale to get the URL, because non-localized routes always have priority
312
+
> when using the `route()` function.
272
313
273
314
### 🚌 Redirect to Routes
274
315
275
-
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.
316
+
Laravel's `Redirector` uses the same `UrlGenerator` as the `route()` function behind the scenes.
317
+
Because we are overriding this class, you can easily redirect to your routes.
276
318
277
319
```php
278
320
return redirect()->route('home'); // non-localized route, redirects to /home
@@ -346,9 +388,11 @@ The above will generate:
346
388
347
389
## 🚏 Route Parameters
348
390
349
-
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.
391
+
Parameter placeholders are not translated via language files. These are values you would provide via the `route()` function.
392
+
The `Lang::uri()` macro will skip any parameter placeholder segment.
350
393
351
-
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.
394
+
If you have a model that uses a route key that is translated in the current locale,
395
+
then you can still simply pass the model to the `route()` function to get translated URL's.
0 commit comments