Skip to content

Commit

Permalink
Improved LocaleMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
libern committed Aug 12, 2016
1 parent fdc8d99 commit f90e276
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/Someline/Support/Middleware/LocaleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit f90e276

Please sign in to comment.