1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
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; } |