Skip to content

Commit a24fa7c

Browse files
author
Marc Cámara
committed
Merge branch 'development' of https://github.com/mcamara/laravel-localization into development
2 parents 3dc778b + 983a27e commit a24fa7c

File tree

2 files changed

+337
-339
lines changed

2 files changed

+337
-339
lines changed

README.md

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ It's recommended that you use Composer, however you can download and install fro
5050

5151
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.
5252

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

5555
```php
5656
...
5757
'Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider'
5858
...
5959
```
6060

61-
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.
6262

6363
```php
6464
...
@@ -68,12 +68,12 @@ You can also add an alias to the list of class aliases in the same app.php
6868

6969
## Usage
7070

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

7373
```php
74-
// app/routes.php
74+
// app/Http/routes.php
7575

76-
Route::group(array('prefix' => LaravelLocalization::setLocale()), function()
76+
Route::group(['prefix' => LaravelLocalization::setLocale()), function()
7777
{
7878
/** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
7979
Route::get('/', function()
@@ -84,33 +84,33 @@ Laravel Localization uses the URL given for the request. In order to achieve thi
8484
Route::get('test',function(){
8585
return View::make('test');
8686
});
87-
});
87+
}];
8888

8989
/** OTHER PAGES THAT SHOULD NOT BE LOCALIZED **/
9090

9191
```
9292

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

9595
```
9696
http://url-to-laravel/en
9797
http://url-to-laravel/es
9898
http://url-to-laravel
9999
```
100100

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

103103
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.
104104

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

107107
### Middleware
108108

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".
110110

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

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

115115
```php
116116
<?php namespace App\Http;
@@ -135,13 +135,13 @@ To do so, you have to register the middleware on the app/Http/Kernel.php laravel
135135

136136

137137
```php
138-
// app/routes.php
138+
// app/Http/routes.php
139139

140140
Route::group(
141-
array(
141+
[
142142
'prefix' => LaravelLocalization::setLocale(),
143143
'middleware' => [ 'localizationRedirect' ]
144-
),
144+
],
145145
function()
146146
{
147147
/** 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
159159

160160
```
161161

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

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

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

168168
## Helpers
169169

@@ -254,9 +254,9 @@ This function will return all supported locales and their properties as an array
254254
```php
255255
/**
256256
* Returns supported languages language key
257-
*
257+
*
258258
* @return array keys of supported languages
259-
*/
259+
*/
260260
public function getSupportedLanguagesKeys()
261261

262262
//Should be called like this:
@@ -284,7 +284,7 @@ This function will return an array with all the keys for the supported locales.
284284
This function will change the application's current locale.
285285
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).
286286

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

289289
### Get Current Locale
290290

@@ -316,7 +316,7 @@ This function will return the key of the current locale.
316316
{{ LaravelLocalization::getCurrentLocaleName() }}
317317
```
318318

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

321321

322322
### Get Current Locale Direction
@@ -333,7 +333,7 @@ This function will return current locale name as string (English/Spanish/Arabic/
333333
{{ LaravelLocalization::getCurrentLocaleDirection() }}
334334
```
335335

336-
This function will return current locale direction as string (ltr/rtl).
336+
This function will return current locale's direction as string (ltr/rtl).
337337

338338

339339
### Get Current Locale Script
@@ -355,7 +355,7 @@ This function will return the [ISO 15924](http://www.unicode.org/iso15924) code
355355

356356
## Creating a language selector
357357

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

360360
```
361361
<ul class="language_bar_chooser">
@@ -374,7 +374,7 @@ If you're supporting multiple locales in your project your going to want to prov
374374

375375
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:
376376

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

379379

380380
```php
@@ -398,13 +398,13 @@ As it is a middleware, first you have to register in on your app/Http/Kernel.php
398398
```
399399

400400
```php
401-
// app/routes.php
401+
// app/Http/routes.php
402402

403403
Route::group(
404-
array(
404+
[
405405
'prefix' => LaravelLocalization::setLocale(),
406406
'middleware' => [ 'localize' ] // Route translate middleware
407-
),
407+
],
408408
function()
409409
{
410410
/** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
@@ -418,60 +418,60 @@ As it is a middleware, first you have to register in on your app/Http/Kernel.php
418418
return View::make('about');
419419
});
420420
Route::get(LaravelLocalization::transRoute('routes.view'),function($id){
421-
return View::make('view',array('id'=>$id));
421+
return View::make('view',['id'=>$id]);
422422
});
423423
});
424424

425425
/** OTHER PAGES THAT SHOULD NOT BE LOCALIZED **/
426426
```
427427
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.
428428

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:
430430
```php
431-
// app/lang/en/routes.php
432-
return array(
431+
// resources/lang/en/routes.php
432+
return [
433433
"about" => "about",
434434
"view" => "view/{id}", //we add a route parameter
435435
// other translated routes
436-
);
436+
];
437437
```
438438
```php
439-
// app/lang/es/routes.php
440-
return array(
439+
// resources/lang/es/routes.php
440+
return [
441441
"about" => "acerca",
442442
"view" => "ver/{id}", //we add a route parameter
443443
// other translated routes
444-
);
444+
];
445445
```
446446

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

449449
## Events
450450

451451
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 :
452452

453-
````
453+
```php
454454
Event::listen('routes.translation', function($locale, $attributes)
455455
{
456456
// Do your magic
457457

458458
return $attributes;
459459
});
460-
````
460+
```
461461

462-
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)
463463

464464
## Config
465465

466466
### Config Files
467467

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

470470
### Service Providers
471471

472-
Otherwise, you can use ConfigServiceProviders (check <a href="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 <a href="https://raw.githubusercontent.com/mcamara/laravel-localization/master/src/config/config.php">this file</a> for more info).
473473

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

476476
```php
477477
<?php namespace App\Providers;
@@ -503,13 +503,11 @@ For example, editing the default config service provider that Laravel loads when
503503

504504
This config would add Catalan and Achinese as languages and override any other previous supported locales and all the other options in the package.
505505

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`).
508507

509508
## Changelog
510509
View changelog here -> [changelog](CHANGELOG.md)
511510

512511
## License
513512

514513
Laravel Localization is an open-sourced laravel package licensed under the MIT license
515-

0 commit comments

Comments
 (0)