Multi-language Controller

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

class HomeC extends Controller
{
    protected $locale = 'zhtw';
    protected $fallback_locale = 'en';

    public function __construct(){
        $this->middleware(function ($request, $next) {
            $locale = $request->input('locale');
            if($locale != false){
                $lang_data = Language::where('code', $locale)->first();
                if($lang_data == false){
                    $locale = $this->locale;
                }
                //Set cookie only when locale has value
                setcookie('locale', $locale, time() + 30*24*60*60);
                App()->setLocale($locale);
            }else{
                $locale = val($_COOKIE, 'locale');
                if($locale != false){
                    App()->setLocale($locale);
                }
            }

            return $next($request);
        });
    }

    private function trans_model($data){
        $locale = App()->getLocale();

        if(is_array($data) == true || is_collection($data) == true){
            foreach ($data as $key => $item) {
                $item->langs($locale, $this->fallback_locale);
            }
        }else{
            $data = $data->langs($locale, $this->fallback_locale);
        }

        return $data;
    }

    public function team(Request $req){
        $data = $this->view_data();

        $admins = Member::withTranslations()
            ->where('category_id', 4)
            ->orderBy('sort_order', 'asc')
            ->get();
        $admins = $this->trans_model($admins);
        $data['admins'] = $admins;
        //dd(App()->getLocale());

        $data['page_title'] = '臺灣館策展工作團隊';

        return view('team', $data);
    }
}

Javascript

        var base_url = '{{BASE_URL}}';

        if (location.protocol != 'https:')
        {
         //location.href = 'https:' + window.location.href.substring(window.location.protocol.length);
        }

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $(document).ready(function(){
            var lang = Cookies.get('locale');
            if(typeof lang == 'undefined' || lang == null){
                lang = browser_lang();
                Cookies.set('lang', lang, { expires: 30 });
                go_lang(lang);
            }
        });
        

        function browser_lang(){
            var lang = navigator.language || navigator.userLanguage;

            if(lang.match(/.*zh.*/i) != null){
                return 'zhtw';
            }else if(lang.match(/.*de.*/i) != null){
                return 'de';
            }else{
                return 'en';
            }
        }

        function go_lang(code){
            window.location.href = '/?locale=' + code;
        }