Skip to content

Instantly share code, notes, and snippets.

@pepzwee
Last active April 1, 2016 08:58
Show Gist options
  • Save pepzwee/ac5a5ba551a7abe7a9b066be240ce74c to your computer and use it in GitHub Desktop.
Save pepzwee/ac5a5ba551a7abe7a9b066be240ce74c to your computer and use it in GitHub Desktop.
<?php
// Add the LanguageMiddleware to $middlewareGroups
// Add it below StartSession Middleware.
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\App\Http\Middleware\LanguageMiddleware::class, // Here's our LanguageMiddleware
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
];
// OR
// If you don't use the "web" middleware make a new one
protected $middlewareGroups = [
'web' => [],
// ...
// add this to the bottom of $middlewareGroups
'language' => [
\Illuminate\Session\Middleware\StartSession::class, // We need sessions for the LanguageMiddleware to work
\App\Http\Middleware\LanguageMiddleware::class, // Here's our LanguageMiddleware
]
];
// PS! Don't use multiple $middlewareGroups in kernel.. lol
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
// Caching is useful if we actually use an API to request the locale to use for an IP
use Cache;
class LanguageController extends Controller
{
// This function will be used in Routes.php
// It changes the Locale in the session
public function switchLang($lang)
{
// Check if valid language
if (in_array($lang, ['en', 'ee', 'ru'])) {
Session::set('applocale', $lang);
}
return redirect()->back();
}
public function queryAPI($ip)
{
// Here It's possible to make a request to an IP Geo API to set the user's language.
// When using an API I recommend caching the response for that IP.
// I just return English by default.
return 'en';
}
}
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Session;
use App\Http\Controllers\LanguageController;
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 in_array(Session::get('applocale'), ['en', 'ee', 'ru'])) {
App::setLocale(Session::get('applocale'));
} else {
$langCtrl = new LanguageController;
$userIP = request()->ip();
$userLang = $langCtrl->queryAPI($userIP);
Session::set('applocale', $userLang);
App::setLocale($userLang);
}
return $next($request);
}
}
<?php
// This switches the language
// You need to put this route in 'web' middleware group or your custom one
// I'm going to use the language middleware group since I made one in Kernel.php
Route::group(['middleware' => ['language']], function() {
Route::get('lang/{lang}', ['as' => 'lang.switch', 'uses' => 'LanguageController@switchLang']);
// Every other Route needs to be here aswell. Since sessions need to be used in all routes!
// Or atleast every route that needs the locale :)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment