Last active
April 1, 2016 08:58
-
-
Save pepzwee/ac5a5ba551a7abe7a9b066be240ce74c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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