Last active
October 2, 2020 10:02
-
-
Save DavidMRGaona/1f27a2ff6ae02c94258262f040395f8d to your computer and use it in GitHub Desktop.
Fragmento para utilizar en un middleware. Este comprueba si existe un locale en la sesión en cuyo caso utilizará ese. En caso contrario utilizara el que existe en la cabecera del servidor. tambien cambia el locale de la librería Carbon.
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\Carbon; | |
use Illuminate\Support\Facades\{App, Config, Session}; | |
class LocalizationMiddleware | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
if (Session::has('locale')) { | |
$locale = Session::get('locale', Config::get('app.locale')); | |
} else { | |
$locale = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2); | |
if ($locale != 'es' && $locale != 'en') { | |
$locale = 'en'; | |
} | |
} | |
App::setLocale($locale); | |
// Set the Carbon locale | |
Carbon::setLocale($locale); | |
return $next($request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment