Skip to content

Commit 032e3ea

Browse files
committed
Update README
1 parent 10f49b9 commit 032e3ea

File tree

1 file changed

+71
-27
lines changed

1 file changed

+71
-27
lines changed

README.md

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@
99

1010
[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/R6R3UQ8V)
1111

12-
#### 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.
1313

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.
1720
- [Generate localized route URL's](#-generate-route-urls) using the `route()` helper.
1821
- [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).
2026
- 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.**
2427

2528
## 🔌 Demo App
2629

@@ -46,7 +49,7 @@ composer require codezero/laravel-localized-routes
4649

4750
#### ☑️ Publish Configuration File
4851

49-
```php
52+
```bash
5053
php artisan vendor:publish --provider="CodeZero\LocalizedRoutes\LocalizedRoutesServiceProvider" --tag="config"
5154
```
5255

@@ -62,11 +65,11 @@ Add any locales you wish to support to your published `config/localized-routes.p
6265
'supported-locales' => ['en', 'nl', 'fr'],
6366
```
6467

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).
6669

6770
##### Using Domains
6871

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:
7073

7174
```php
7275
'supported-locales' => [
@@ -94,32 +97,36 @@ Setting this option to `'en'` will result, for example, in URL's like this:
9497
9598
#### ☑️ Use Middleware to Update App Locale
9699

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.
98102

99103
**⚠️ Important note for Laravel 6+**
100104

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`:
102107

103108
```php
104109
protected $middlewarePriority = [
105110
\Illuminate\Session\Middleware\StartSession::class, // <= after this
106111
//...
107-
\CodeZero\LocalizedRoutes\Middleware\LocalizedRouteLocaleHandler::class,
112+
\CodeZero\LocalizedRoutes\Middleware\SetLocale::class,
108113
//...
109114
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
110115
];
111116
```
112117

113118
You can then enable the middleware in a few ways:
114119

115-
**For every route, via our config file**
120+
**For every localized route, via our config file**
116121

117-
Simply set the option to `true`:
122+
Simply set the option to `true` to add the middleware to every localized route:
118123

119124
```php
120125
'use_locale_middleware' => true
121126
```
122127

128+
> This will not add the middleware to non-localized routes!
129+
123130
**OR, for every route using the `web` middleware group**
124131

125132
You can manually add the middleware to the `$middlewareGroups` array in `app/Http/Kernel.php`:
@@ -129,7 +136,7 @@ protected $middlewareGroups = [
129136
'web' => [
130137
\Illuminate\Session\Middleware\StartSession::class, // <= after this
131138
//...
132-
\CodeZero\LocalizedRoutes\Middleware\LocalizedRouteLocaleHandler::class,
139+
\CodeZero\LocalizedRoutes\Middleware\SetLocale::class,
133140
//...
134141
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
135142
],
@@ -145,11 +152,11 @@ Route::localized(function () {
145152

146153
Route::get('about', AboutController::class.'@index')
147154
->name('about')
148-
->middleware(\CodeZero\LocalizedRoutes\Middleware\LocalizedRouteLocaleHandler::class);
155+
->middleware(\CodeZero\LocalizedRoutes\Middleware\SetLocale::class);
149156

150157
Route::group([
151158
'as' => 'admin.',
152-
'middleware' => [\CodeZero\LocalizedRoutes\Middleware\LocalizedRouteLocaleHandler::class],
159+
'middleware' => [\CodeZero\LocalizedRoutes\Middleware\SetLocale::class],
153160
], function () {
154161

155162
Route::get('admin/reports', ReportsController::class.'@index')
@@ -160,9 +167,34 @@ Route::localized(function () {
160167
});
161168
```
162169

170+
#### ☑️ 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+
163194
#### ☑️ Set Options for the Current Localized Route Group
164195

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.
197+
This will override the config file settings.
166198

167199
```php
168200
Route::localized(function () {
@@ -200,7 +232,9 @@ Route::localized(function () {
200232
});
201233
```
202234

203-
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.
204238

205239
| URI | Name |
206240
| ----------------- | ---------------------- |
@@ -220,7 +254,9 @@ If you set `omit_url_prefix_for_locale` to `'en'` in the configuration file, the
220254
| /admin/reports | en.admin.reports.index |
221255
| /nl/admin/reports | nl.admin.reports.index |
222256

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.
224260

225261
### 🚕 Generate Route URL's
226262

@@ -236,7 +272,10 @@ $url = route(app()->getLocale().'.admin.reports.index');
236272

237273
##### 👍 A much nicer way...
238274

239-
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.
240279

241280
```php
242281
$url = route('admin.reports.index'); // current locale
@@ -268,11 +307,14 @@ $url = route('en.about'); // /en/about
268307
$url = route('en.about', [], true, 'nl'); // /nl/about
269308
```
270309

271-
> **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.
272313
273314
### 🚌 Redirect to Routes
274315

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.
276318

277319
```php
278320
return redirect()->route('home'); // non-localized route, redirects to /home
@@ -346,9 +388,11 @@ The above will generate:
346388
347389
## 🚏 Route Parameters
348390

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.
350393

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.
352396

353397
An example...
354398

0 commit comments

Comments
 (0)