Skip to content

Instantly share code, notes, and snippets.

@huiralb
Created March 15, 2016 04:14
Show Gist options
  • Save huiralb/30b7e20d4e845e897fa8 to your computer and use it in GitHub Desktop.
Save huiralb/30b7e20d4e845e897fa8 to your computer and use it in GitHub Desktop.
Laravel Language ( Localization )
<?php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\App\Http\Middleware\LanguageMiddleware::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
// ...
];
<?php
// app/Http/Controllers/LanguageController.php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Config;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
class LanguageController extends Controller
{
public function switchLang($lang)
{
if (array_key_exists($lang, Config::get('languages'))) {
Session::set('applocale', $lang);
}
return Redirect::back();
}
}
<?php
// app/Http/Middleware/LanguageMiddleware.php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;
class LanguageMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (Session::has('applocale') AND array_key_exists(Session::get('applocale'), Config::get('languages')))
{
$lang = Session::get('applocale');
App::setLocale($lang);
}
else {
// This is optional as Laravel will automatically set the fallback language if there is none specified
App::setLocale(Config::get('app.fallback_locale'));
}
return $next($request);;
}
}
<?php
// config/languages.php
return [
'en' => 'English',
'fr' => 'Français',
];
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
{{ Config::get('languages')[App::getLocale()] }}
</a>
<ul class="dropdown-menu">
@foreach (Config::get('languages') as $lang => $language)
@if ($lang != App::getLocale())
<li>
<a href="{{ route('lang.switch', $lang) }}">{{$language}}</a>
</li>
@endif
@endforeach
</ul>
</li>
<?php
// app/Http/Controllers/PageController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;
class PageController extends Controller
{
protected $lang;
public function __construct()
{
// default locale is en
Session::has('applocale') ?: Session::set('applocale', 'en');
$this->lang = Session::get('applocale');
}
public function index(Request $request)
{
return View('pages.'.$this->lang.'.welcome');
}
}
<?php
// app/Http/routes.php
// .......
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function () {
Route::get('/', 'PageController@index');
Route::get('/lang/{lang}', ['as' => 'lang.switch', 'uses'=>'LanguageController@switchLang']);
});
resources/views/pages/
| fr
| home
| en
| home
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment