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
Copy file name to clipboardExpand all lines: README.md
+44-46Lines changed: 44 additions & 46 deletions
Original file line number
Diff line number
Diff line change
@@ -50,15 +50,15 @@ It's recommended that you use Composer, however you can download and install fro
50
50
51
51
Laravel Localization comes with a service provider for Laravel. You'll need to add it to your `composer.json` as mentioned in the above steps, then register the service provider with your application.
52
52
53
-
Open `app/config/app.php` and find the `providers` key. Add `LaravelLocalizationServiceProvider` to the array.
53
+
Open `config/app.php` and find the `providers` key. Add `LaravelLocalizationServiceProvider` to the array.
You can also add an alias to the list of class aliases in the same app.php
61
+
You can also add an alias to the list of class aliases in the same file.
62
62
63
63
```php
64
64
...
@@ -68,12 +68,12 @@ You can also add an alias to the list of class aliases in the same app.php
68
68
69
69
## Usage
70
70
71
-
Laravel Localization uses the URL given for the request. In order to achieve this purpose, a group should be added into the routes.php file. It will filter all pages that must be localized.
71
+
Laravel Localization uses the URL given for the request. In order to achieve this purpose, a route group should be added into the `routes.php` file. It will filter all pages that must be localized.
/** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
79
79
Route::get('/', function()
@@ -84,33 +84,33 @@ Laravel Localization uses the URL given for the request. In order to achieve thi
84
84
Route::get('test',function(){
85
85
return View::make('test');
86
86
});
87
-
});
87
+
}];
88
88
89
89
/** OTHER PAGES THAT SHOULD NOT BE LOCALIZED **/
90
90
91
91
```
92
92
93
-
Once this group is added to the routes file, a user can access all locales added into 'supportedLocales' ('en' and 'es' by default, look at the config section to change that option). For example, a user can now access to two different locales, using the following addresses:
93
+
Once this route group is added to the routes file, a user can access all locales added into `supportedLocales` ('en' and 'es' by default, look at the config section to change that option). For example, a user can now access two different locales, using the following addresses:
94
94
95
95
```
96
96
http://url-to-laravel/en
97
97
http://url-to-laravel/es
98
98
http://url-to-laravel
99
99
```
100
100
101
-
If the locale is not present in the url or it is not defined in 'supportedLocales', the system will use the application default locale or the user's browser default locale (if defined in config file).
101
+
If the locale is not present in the url or it is not defined in `supportedLocales`, the system will use the application default locale or the user's browser default locale (if defined in config file).
102
102
103
103
Once the locale is defined, the locale variable will be stored in a session, so it is not necessary to write the /lang/ section in the url after defining it once, using the last known locale for the user. If the user accesses to a different locale this session value would be changed, translating any other page he visits with the last chosen locale.
104
104
105
-
Templates files and all locale files should follow the [Lang class](http://laravel.com/docs/localization).
105
+
Template files and all locale files should follow the [Lang class](http://laravel.com/docs/5.0/localization).
106
106
107
107
### Middleware
108
108
109
-
Moreover, this package includes a middleware obkect to redirect all "non-localized" routes to a "localized" one.
109
+
Moreover, this package includes a middleware object to redirect all "non-localized" routes to the corresponding "localized".
110
110
111
-
So, if a user accesses to http://url-to-laravel/test and the system have this middleware actived and 'en' as a current locale for this user, it would redirect (301) him automatically to http://url-to-laravel/en/test. This is mainly used to avoid duplicate content and improve SEO performance.
111
+
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 (301) him automatically to http://url-to-laravel/en/test. This is mainly used to avoid duplicate content and improve SEO performance.
112
112
113
-
To do so, you have to register the middleware on the app/Http/Kernel.php laravel file like this:
113
+
To do so, you have to register the middleware in the `app/Http/Kernel.php` file like this:
114
114
115
115
```php
116
116
<?php namespace App\Http;
@@ -135,13 +135,13 @@ To do so, you have to register the middleware on the app/Http/Kernel.php laravel
135
135
136
136
137
137
```php
138
-
// app/routes.php
138
+
// app/Http/routes.php
139
139
140
140
Route::group(
141
-
array(
141
+
[
142
142
'prefix' => LaravelLocalization::setLocale(),
143
143
'middleware' => [ 'localizationRedirect' ]
144
-
),
144
+
],
145
145
function()
146
146
{
147
147
/** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
@@ -159,11 +159,11 @@ To do so, you have to register the middleware on the app/Http/Kernel.php laravel
159
159
160
160
```
161
161
162
-
In order to active it, you just have to attach this middleware to the routes you want to be accessible localized.
162
+
In order to activate it, you just have to attach this middleware to the routes you want to be accessible localized.
163
163
164
-
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).
164
+
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).
165
165
166
-
**IMPORTANT** - When hideDefaultLocaleInURL is set to true, the unlocalized root is treated as the applications default locale ```app.locale```. Because of this language negotiation using the Accept-Language header will **NEVER** occur when hideDefaultLocaleInURL is true.
166
+
**IMPORTANT** - When `hideDefaultLocaleInURL` is set to true, the unlocalized root is treated as the applications default locale `app.locale`. Because of this language negotiation using the Accept-Language header will **NEVER** occur when `hideDefaultLocaleInURL` is true.
167
167
168
168
## Helpers
169
169
@@ -254,9 +254,9 @@ This function will return all supported locales and their properties as an array
254
254
```php
255
255
/**
256
256
* Returns supported languages language key
257
-
*
257
+
*
258
258
* @return array keys of supported languages
259
-
*/
259
+
*/
260
260
public function getSupportedLanguagesKeys()
261
261
262
262
//Should be called like this:
@@ -284,7 +284,7 @@ This function will return an array with all the keys for the supported locales.
284
284
This function will change the application's current locale.
285
285
If the locale is not passed, the locale will be determined via a cookie (if stored previously), the session (if stored previously), browser Accept-Language header or the default application locale (depending on your config file).
286
286
287
-
The function have to be called in the prefix of any route that should be translated (see Filters sections for further information).
287
+
The function has to be called in the prefix of any route that should be translated (see Filters sections for further information).
288
288
289
289
### Get Current Locale
290
290
@@ -316,7 +316,7 @@ This function will return the key of the current locale.
316
316
{{ LaravelLocalization::getCurrentLocaleName() }}
317
317
```
318
318
319
-
This function will return current locale name as string (English/Spanish/Arabic/ ..etc).
319
+
This function will return current locale's name as string (English/Spanish/Arabic/ ..etc).
320
320
321
321
322
322
### Get Current Locale Direction
@@ -333,7 +333,7 @@ This function will return current locale name as string (English/Spanish/Arabic/
This function will return current locale direction as string (ltr/rtl).
336
+
This function will return current locale's direction as string (ltr/rtl).
337
337
338
338
339
339
### Get Current Locale Script
@@ -355,7 +355,7 @@ This function will return the [ISO 15924](http://www.unicode.org/iso15924) code
355
355
356
356
## Creating a language selector
357
357
358
-
If you're supporting multiple locales in your project your going to want to provide the users with a way to change language. Below is a simple example of blade template code you can use to create your own language selector.
358
+
If you're supporting multiple locales in your project you will probably want to provide the users with a way to change language. Below is a simple example of blade template code you can use to create your own language selector.
359
359
360
360
```
361
361
<ul class="language_bar_chooser">
@@ -374,7 +374,7 @@ If you're supporting multiple locales in your project your going to want to prov
374
374
375
375
You can adapt your URLs depending on the language you want to show them. For example, http://url/en/about and http://url/es/acerca (acerca is about in spanish) or http://url/en/view/5 and http://url/es/ver/5 (view == ver in spanish) would be redirected to the same controller using the proper filter and setting up the translation files as follows:
376
376
377
-
As it is a middleware, first you have to register in on your app/Http/Kernel.php file like this:
377
+
As it is a middleware, first you have to register in on your `app/Http/Kernel.php` file like this:
378
378
379
379
380
380
```php
@@ -398,13 +398,13 @@ As it is a middleware, first you have to register in on your app/Http/Kernel.php
In the routes file you just have to add the `LaravelLocalizationRoutes` filter and the `LaravelLocalization::transRoute` function to every route you want to translate using the translation key.
428
428
429
-
Then you have to create the translation files and add there every key you want to translate. I suggest you to create a routes.php file inside your app/lang/language_abbreviation folder. For the previous example, I have created two translations files, these two files would look like:
429
+
Then you have to create the translation files and add there every key you want to translate. I suggest to create a routes.php file inside your resources/lang/language_abbreviation folder. For the previous example, I have created two translations files, these two files would look like:
430
430
```php
431
-
// app/lang/en/routes.php
432
-
return array(
431
+
// resources/lang/en/routes.php
432
+
return [
433
433
"about" => "about",
434
434
"view" => "view/{id}", //we add a route parameter
435
435
// other translated routes
436
-
);
436
+
];
437
437
```
438
438
```php
439
-
// app/lang/es/routes.php
440
-
return array(
439
+
// resources/lang/es/routes.php
440
+
return [
441
441
"about" => "acerca",
442
442
"view" => "ver/{id}", //we add a route parameter
443
443
// other translated routes
444
-
);
444
+
];
445
445
```
446
446
447
-
Once files are saved, you can access to http://url/en/about , http://url/es/acerca , http://url/en/view/5 and http://url/es/ver/5 without any problem. The getLanguageBar function would work as desired and it will translate the routes to all translated languages (don't forget to add any new route to the translation file).
447
+
Once files are saved, you can access to http://url/en/about , http://url/es/acerca , http://url/en/view/5 and http://url/es/ver/5 without any problem. The `getLanguageBar` function would work as desired and it will translate the routes to all translated languages (don't forget to add any new route to the translation file).
448
448
449
449
## Events
450
450
451
451
You can capture the URL parameters during translation if you wish to translate them too. To do so, just create an event listener for the `routes.translation` event like so :
Be sure to pass the locale and the attributes as parameters for your closure. You can also use Event Subscribers, see: [http://laravel.com/docs/events#event-subscribers](http://laravel.com/docs/events#event-subscribers)
462
+
Be sure to pass the locale and the attributes as parameters to the closure. You may also use Event Subscribers, see: [http://laravel.com/docs/events#event-subscribers](http://laravel.com/docs/events#event-subscribers)
463
463
464
464
## Config
465
465
466
466
### Config Files
467
467
468
-
In order to edit the default configuration for this plugin you can execute ```php artisan vendor:publish``` . After that, config/laravellocalization.php would be created. Inside this file you would find all the fields that can be editable in this plugin.
468
+
In order to edit the default configuration for this package you may execute `php artisan vendor:publish` . After that, config/laravellocalization.php will be created. Inside this file you will find all the fields that can be edited in this package.
469
469
470
470
### Service Providers
471
471
472
-
Otherwise, you can use ConfigServiceProviders (check <ahref="https://raw.githubusercontent.com/mcamara/laravel-localization/master/src/config/config.php">this file</a> for more info).
472
+
Otherwise, you can use `ConfigServiceProviders` (check <ahref="https://raw.githubusercontent.com/mcamara/laravel-localization/master/src/config/config.php">this file</a> for more info).
473
473
474
-
For example, editing the default config service provider that Laravel loads when it's installed. This file is placed in app/providers/ConfigServicePovider.php and would look like this:
474
+
For example, editing the default config service provider that Laravel loads when it's installed. This file is placed in `app/providers/ConfigServicePovider.php` and would look like this:
475
475
476
476
```php
477
477
<?php namespace App\Providers;
@@ -503,13 +503,11 @@ For example, editing the default config service provider that Laravel loads when
503
503
504
504
This config would add Catalan and Achinese as languages and override any other previous supported locales and all the other options in the package.
505
505
506
-
You can create your own config providers and add them on your application config file ( check the providers array in config/app.php ).
507
-
506
+
You can create your own config providers and add them on your application config file (check the providers array in `config/app.php`).
508
507
509
508
## Changelog
510
509
View changelog here -> [changelog](CHANGELOG.md)
511
510
512
511
## License
513
512
514
513
Laravel Localization is an open-sourced laravel package licensed under the MIT license
0 commit comments