<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;

class Language
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $lang = $request->header('lang');
//        App::setLocale($lang);  //配置默认语言
//        return $next($request);
        if(isset($lang)){
            $request->session()->put('language',$lang);
        }

        file_put_contents('request.log',date('Y-m-d H:i:s').'---language-handle-'.$request->session()->has('language').PHP_EOL,FILE_APPEND);
        if ($request->session()->has('language') ) {

            App::setLocale($request->session()->get('language'));
        } else {

            App::setLocale(Config::get('app.locale'));
        }
       // dd(Session::get('language'));
        return $next($request);
    }
}
