<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class SetLocale
{
    public function handle(Request $request, Closure $next)
    {
        $locale = $request->lang;
        if (in_array($locale, ['en', 'ar'])) {
            App::setLocale($locale);
            $request->session()->put('locale', $locale);
        } elseif ($request->session()->has('locale')) {
            App::setLocale($request->session()->get('locale'));
        }
        return $next($request);
    }
}
