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
* 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
Copy file name to clipboardExpand all lines: README.md
+39-34
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,9 @@ Easy i18n localization for Laravel, an useful tool to combine with Laravel local
44
44
45
45
Install the package via composer: `composer require mcamara/laravel-localization`
46
46
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:
48
50
49
51
Register the service provider in `config/app.php`
50
52
@@ -151,11 +153,38 @@ Template files and all locale files should follow the [Lang class](http://larave
151
153
152
154
### Middleware
153
155
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`.
155
176
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
157
178
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:
159
188
160
189
```php
161
190
<?php namespace App\Http;
@@ -170,18 +199,16 @@ class Kernel extends HttpKernel {
/** 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
+
//...
202
221
});
203
222
204
223
/** OTHER PAGES THAT SHOULD NOT BE LOCALIZED **/
205
224
206
225
```
207
226
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
-
222
227
### Map your own custom lang url segments
223
228
224
229
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 ```
// 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
+
//
301
310
// If false, system will take app.php locale attribute
302
311
'useAcceptLanguageHeader' => true,
303
312
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.
306
317
//
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).
309
324
//
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.
310
328
'hideDefaultLocaleInURL' => false,
311
329
312
330
// If you want to display the locales in particular order in the language selector you should write the order here.
313
331
//CAUTION: Please consider using the appropriate locale code otherwise it will not work
314
332
//Example: 'localesOrder' => ['es','en'],
315
333
'localesOrder' => [],
316
-
334
+
317
335
// 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']).
318
336
'localesMapping' => [],
319
-
337
+
320
338
// Locale suffix for LC_TIME and LC_MONETARY
321
339
// Defaults to most common ".UTF-8". Set to blank on Windows systems, change to ".utf8" on CentOS and similar.
0 commit comments