From f90e2764f6f962ca9fb6d83bc276f474afd1f5ac Mon Sep 17 00:00:00 2001 From: Libern Date: Fri, 12 Aug 2016 14:50:48 +0800 Subject: [PATCH] Improved LocaleMiddleware --- .../Support/Middleware/LocaleMiddleware.php | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Someline/Support/Middleware/LocaleMiddleware.php b/src/Someline/Support/Middleware/LocaleMiddleware.php index c4dd2ae..7f63575 100644 --- a/src/Someline/Support/Middleware/LocaleMiddleware.php +++ b/src/Someline/Support/Middleware/LocaleMiddleware.php @@ -5,23 +5,43 @@ use Carbon\Carbon; use Closure; use LaravelLocalization; -use Mcamara\LaravelLocalization\LanguageNegotiator; class LocaleMiddleware { /** * Handle an incoming request. * - * @param \Illuminate\Http\Request $request - * @param \Closure $next + * @param \Illuminate\Http\Request $request + * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { - // get current locale + // get supported locales + $supportedLocales = LaravelLocalization::getSupportedLocales(); + + // 1. get current locale $locale = LaravelLocalization::getCurrentLocale(); + // 2. check from session + $sessionLocale = session('someline-locale'); + if (!empty($sessionLocale)) { + // if supported + if (is_array($supportedLocales) && isset($supportedLocales[$sessionLocale])) { + $locale = $sessionLocale; + } + } + + // 3. check from lang + $lang = $request->get('lang'); + if (!empty($lang)) { + // if supported + if (is_array($supportedLocales) && isset($supportedLocales[$lang])) { + $locale = $lang; + } + } + // set locale LaravelLocalization::setLocale($locale);