<?php

namespace App\Http\Middleware;

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

class SetLanguage
{
    public function handle($request, Closure $next)
    {
        if ($request->user()) {
            $language = $request->user()->language;
            //默认中文
            $language_preference = 'zh-CN';
            //language为1是英文
            if($language == 1){
                $language_preference = 'en';
            }else if($language == 2){
                $language_preference = 'zh-TW';
            }
            if ($language_preference) {
                App::setLocale($language_preference);
            }
        }

        return $next($request);
    }
}