PHPackages                             nguyen930411/laravel-localization - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. nguyen930411/laravel-localization

ActiveLibrary[Localization &amp; i18n](/categories/localization)

nguyen930411/laravel-localization
=================================

Easy localization for Laravel

01.1kPHP

Since Mar 28Pushed 4y ago1 watchersCompare

[ Source](https://github.com/nguyen930411/laravel-localization)[ Packagist](https://packagist.org/packages/nguyen930411/laravel-localization)[ RSS](/packages/nguyen930411-laravel-localization/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Localization
====================

[](#laravel-localization)

Easy i18n localization for Laravel, an useful tool to combine with Laravel localization classes.

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
    - [Composer](#composer)
    - [Manually](#manually)
    - [Laravel](#laravel)
- [Usage](#usage)
    - [Middleware](#middleware)
- [Helpers](#helpers)
- [Translated Routes](#translated-routes)
- [Config](#config)
    - [Config files](#config-files)
    - [Service providers](#service-providers)

Laravel compatibility
---------------------

[](#laravel-compatibility)

Laravel 5 is released!!

Laravellaravel-localization5.0.x/5.1.xdev-masterInstallation
------------

[](#installation)

Install the package via composer: `composer require nguyen930411/laravel-localization`

Register the ServiceProvider in `config/app.php`

```
        'providers' => [
		// [...]
                Nguyen930411\LaravelLocalization\LaravelLocalizationServiceProvider::class,
        ],
```

You may also register the `LaravelLocalization` Facade:

```
        'aliases' => [
		// [...]
                'LaravelLocalization' => Nguyen930411\LaravelLocalization\Facades\LaravelLocalization::class,
        ],
```

Usage
-----

[](#usage)

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.

```
// app/Http/routes.php

Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
	/** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
	Route::get('/', function()
	{
		return View::make('hello');
	});

	Route::get('test',function(){
		return View::make('test');
	});
});

/** OTHER PAGES THAT SHOULD NOT BE LOCALIZED **/
```

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:

```
http://url-to-laravel/en
http://url-to-laravel/es
http://url-to-laravel

```

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

Once the locale is defined, the locale variable will be stored in a session (if the middleware is enabled), 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.

Template files and all locale files should follow the [Lang class](http://laravel.com/docs/5.0/localization).

### Middleware

[](#middleware)

Moreover, this package includes a middleware object to redirect all "non-localized" routes to the corresponding "localized".

So, if a user navigates to  and the system has this middleware active and 'en' as the current locale for this user, it would redirect (301) him automatically to . This is mainly used to avoid duplicate content and improve SEO performance.

To do so, you have to register the middleware in the `app/Http/Kernel.php` file like this:

```
