Skip to content

Commit 00ed4af

Browse files
iwasherefirst2Marc Cámara
authored and
Marc Cámara
committed
Improve config description and readme.md (#666)
* Improve config description and readme.md Many issues show that it is difficult to understand how `hideDefaultLocaleInURL` and `useAcceptLanguageHeader` work, especially how the middleware will effect them and if they are used in combination. Also the description of hideDefaultLocaleInURL was outdated in the config file. This commit is an attempt to be more clear about these configuration setup and the behaviour of the associated middleware. * Minor adjustments to Readme.md
1 parent 4f7e882 commit 00ed4af

File tree

2 files changed

+64
-41
lines changed

2 files changed

+64
-41
lines changed

README.md

+39-34
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ Easy i18n localization for Laravel, an useful tool to combine with Laravel local
4444

4545
Install the package via composer: `composer require mcamara/laravel-localization`
4646

47-
In Laravel 5.5, the service provider and facade will automatically get registered. For older versions of the framework, follow the steps below:
47+
### For Laravel 5.4 and below:
48+
49+
For older versions of the framework, follow the steps below:
4850

4951
Register the service provider in `config/app.php`
5052

@@ -151,11 +153,38 @@ Template files and all locale files should follow the [Lang class](http://larave
151153

152154
### Middleware
153155

154-
Moreover, this package includes a middleware object to redirect all "non-localized" routes to the corresponding "localized".
156+
The packages ships with useful middleware. The behavior depends on the settings of `hideDefaultLocaleInURL`
157+
and `useAcceptLanguageHeader` in `config/laravellocalization.php`:
158+
159+
#### LocaleSessionRedirect
160+
161+
Whenever a locale is present in the url, it will be stored in the session by this middleware.
162+
163+
In there is no locale present in the url, then this middleware will check the following
164+
165+
- If no locale is saved in session and `useAcceptLanguageHeader` is set to true, compute locale from browser and redirect to url with locale.
166+
- If a locale is saved in session redirect to url with locale, unless its the default locale and `hideDefaultLocaleInURL` is set to true.
167+
168+
For example, if a user navigates to http://url-to-laravel/test and `en` is the current locale, it would redirect him automatically to http://url-to-laravel/en/test.
169+
170+
#### LaravelLocalizationRedirectFilter
171+
172+
When the default locale is present in the url and `hideDefaultLocaleInURL` is set to true, then the middleware redirects to the url without locale.
173+
174+
For example, if `es` is the default locale, then http://url-to-laravel/es/test would be redirected to http://url-to-laravel/test and the`App::getLocale()` would be
175+
set to `es`.
155176

156-
So, if a user navigates to http://url-to-laravel/test and the system has this middleware active and 'en' as the current locale for this user, it would redirect (302) him automatically to http://url-to-laravel/en/test. This is mainly used to avoid duplicate content and improve SEO performance.
177+
#### LaravelLocalizationViewPath
157178

158-
To do so, you have to register the middleware in the `app/Http/Kernel.php` file like this:
179+
Register this middleware to set current locale as view-base-path.
180+
181+
Now you can wrap your views in language-based folders like the translation files.
182+
183+
`resources/views/en/`, `resources/views/fr`, ...
184+
185+
#### Register Middleware
186+
187+
You may register the above middleware in the `app/Http/Kernel.php` file and in the `Route:group` like this:
159188

160189
```php
161190
<?php namespace App\Http;
@@ -170,18 +199,16 @@ class Kernel extends HttpKernel {
170199
*/
171200
protected $routeMiddleware = [
172201
/**** OTHER MIDDLEWARE ****/
173-
'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
174-
'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
175-
'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
176-
'localeViewPath' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class
177-
// REDIRECTION MIDDLEWARE
202+
'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
203+
'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
204+
'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
205+
'localeViewPath' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class
178206
];
179207
}
180208
```
181209

182-
183210
```php
184-
// app/Http/routes.php
211+
// routes/web.php
185212

186213
Route::group(
187214
[
@@ -190,35 +217,13 @@ Route::group(
190217
],
191218
function()
192219
{
193-
/** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
194-
Route::get('/', function()
195-
{
196-
return View::make('hello');
197-
});
198-
199-
Route::get('test',function(){
200-
return View::make('test');
201-
});
220+
//...
202221
});
203222

204223
/** OTHER PAGES THAT SHOULD NOT BE LOCALIZED **/
205224

206225
```
207226

208-
In order to activate it, you just have to attach this middleware to the routes you want to be accessible localized.
209-
210-
If you want to hide the default locale but always show other locales in the url, switch the `hideDefaultLocaleInURL` config value to true. Once it's true, if the default locale is en (english) all URLs containing /en/ would be redirected to the same url without this fragment '/' but maintaining the locale as en (English).
211-
212-
When `hideDefaultLocaleInURL` and `useAcceptLanguageHeader` are both set to true,then the language negotiation using the Accept-Language header will only occur while the session('locale') is empty. After negotiation, the session('locale') will be set accordingly and will not be called again.
213-
214-
### Set current locale as view-base-path
215-
216-
To set the current locale as view-base-path, simply register the localeViewPath-middlware in your Kernel.php, like it is descriped above.
217-
218-
Now you can wrap your views in language-based folders like the translation files.
219-
220-
`resources/views/en/`, `resources/views/fr`, ...
221-
222227
### Map your own custom lang url segments
223228

224229
As you can modify the supportedLocales even by renaming their keys, it is possible to use the string ```uk``` instead of ```en-GB``` to provide custom lang url segments. Of course, you need to prevent any collisions with already existing keys and should stick to the convention as long as possible. But if you are using such a custom key, you have to store your mapping to the ```localesMapping``` array. This ```

src/config/config.php

+25-7
Original file line numberDiff line numberDiff line change
@@ -297,26 +297,44 @@
297297
//'ko' => ['name' => 'Korean', 'script' => 'Hang', 'native' => '한국어', 'regional' => 'ko_KR'],
298298
],
299299

300-
// Negotiate for the user locale using the Accept-Language header if it's not defined in the URL?
300+
// Requires middleware `LaravelSessionRedirect.php`.
301+
//
302+
// Automatically determine locale from browser (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language)
303+
// on first call if it's not defined in the URL. Redirect user to computed localized url.
304+
// For example, if users browser language is `de`, and `de` is active in the array `supportedLocales`,
305+
// the `/about` would be redirected to `/de/about`.
306+
//
307+
// The locale will be stored in session and only be computed from browser
308+
// again if the session expires.
309+
//
301310
// If false, system will take app.php locale attribute
302311
'useAcceptLanguageHeader' => true,
303312

304-
// If LaravelLocalizationRedirectFilter is active and hideDefaultLocaleInURL
305-
// is true, the url would not have the default application language
313+
// If `hideDefaultLocaleInURL` is true, then a url without locale
314+
// is identical with the same url with default locale.
315+
// For example, if `en` is default locale, then `/en/about` and `/about`
316+
// would be identical.
306317
//
307-
// IMPORTANT - When hideDefaultLocaleInURL is set to true, the unlocalized root is treated as the applications default locale "app.locale".
308-
// Because of this language negotiation using the Accept-Language header will NEVER occur when hideDefaultLocaleInURL is true.
318+
// If in addition the middleware `LaravelLocalizationRedirectFilter` is active, then
319+
// every url with default locale is redirected to url without locale.
320+
// For example, `/en/about` would be redirected to `/en`.
321+
// It is recommended to use `hideDefaultLocaleInURL` only in
322+
// combination with the middleware `LaravelLocalizationRedirectFilter`
323+
// to avoid duplicate content (SEO).
309324
//
325+
// If `useAcceptLanguageHeader` is true, then the first time
326+
// the locale will be determined from browser and redirect to that language.
327+
// After that, `hideDefaultLocaleInURL` behaves as usual.
310328
'hideDefaultLocaleInURL' => false,
311329

312330
// If you want to display the locales in particular order in the language selector you should write the order here.
313331
//CAUTION: Please consider using the appropriate locale code otherwise it will not work
314332
//Example: 'localesOrder' => ['es','en'],
315333
'localesOrder' => [],
316-
334+
317335
// If you want to use custom lang url segments like 'at' instead of 'de-AT', you can use the mapping to tallow the LanguageNegotiator to assign the descired locales based on HTTP Accept Language Header. For example you want ot use 'at', so map HTTP Accept Language Header 'de-AT' to 'at' (['de-AT' => 'at']).
318336
'localesMapping' => [],
319-
337+
320338
// Locale suffix for LC_TIME and LC_MONETARY
321339
// Defaults to most common ".UTF-8". Set to blank on Windows systems, change to ".utf8" on CentOS and similar.
322340
'utf8suffix' => env('LARAVELLOCALIZATION_UTF8SUFFIX', '.UTF-8'),

0 commit comments

Comments
 (0)