Просмотр исходного кода

feat(locale): add a route for lang switch

hardywu 7 лет назад
Родитель
Сommit
3dee55b903

+ 7 - 1
app/Http/Controllers/UserController.php

@@ -50,7 +50,6 @@ class UserController extends Controller
     public function index(Request $request)
     {
         $user = $request->session()->get('user');
-        $request->session()->put("locale", $request->input('locale', 'en'));
 
         $user = User::query()->where('id', $user['id'])->first();
         $user->totalTransfer = flowAutoShow($user->transfer_enable);
@@ -1172,4 +1171,11 @@ class UserController extends Controller
             return Response::json(['status' => 'fail', 'data' => '', 'message' => '充值失败']);
         }
     }
+
+    public function switchLang(Request $request, $locale)
+    {
+        $request->session()->put("locale", $locale);
+        return Redirect::back();
+    }
+
 }

+ 1 - 1
app/Http/Kernel.php

@@ -18,7 +18,6 @@ class Kernel extends HttpKernel
         \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
         \App\Http\Middleware\TrimStrings::class,
         \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
-        \App\Http\Middleware\SetLocale::class,
     ];
 
     /**
@@ -32,6 +31,7 @@ class Kernel extends HttpKernel
             \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
             \Illuminate\Session\Middleware\StartSession::class,
             // \Illuminate\Session\Middleware\AuthenticateSession::class,
+            \App\Http\Middleware\SetLocale::class,
             \Illuminate\View\Middleware\ShareErrorsFromSession::class,
             \App\Http\Middleware\VerifyCsrfToken::class,
             \Illuminate\Routing\Middleware\SubstituteBindings::class,

+ 9 - 2
app/Http/Middleware/SetLocale.php

@@ -4,6 +4,7 @@ namespace App\Http\Middleware;
 
 use Closure;
 use Session;
+use Illuminate\Support\Facades\App;
 
 class SetLocale {
     /**
@@ -15,9 +16,15 @@ class SetLocale {
      */
     public function handle($request, Closure $next)
     {
-        if(Session::has('locale'))
+        if (Session::has('locale'))
         {
-            app()->setLocale(Session::get('locale'));
+            App::setLocale(Session::get('locale'));
+        }
+
+        if ($request->query('locale'))
+        {
+            Session::put('locale', $request->query('locale'));
+            App::setLocale($request->query('locale'));
         }
 
         return $next($request);

+ 7 - 5
resources/views/login.blade.php

@@ -37,11 +37,13 @@
 <!-- END LOGO -->
 <!-- BEGIN LOGIN -->
 <div class="content">
-    @if(Session::get('locale') == 'zh-CN')
-        <a href="/?locale=en">English</a>
-    @else
-        <a href="/?locale=zh-CN">中文</a>
-    @endif
+    <nav style="padding-bottom: 20px;text-align: center;">
+        @if(app()->getLocale() == 'zh-CN')
+            <a style="border: 1px solid;padding: 10px;" href="{{url('lang', ['locale' => 'en'])}}">English</a>
+        @else
+            <a style="border: 1px solid;padding: 10px;" href="{{url('lang', ['locale' => 'zh-CN'])}}">中文</a>
+        @endif
+    </nav>
     <!-- BEGIN LOGIN FORM -->
     <form class="login-form" action="{{url('login')}}" method="post">
         <div class="alert alert-danger display-hide">

+ 7 - 5
resources/views/register.blade.php

@@ -37,11 +37,13 @@
 <!-- END LOGO -->
 <!-- BEGIN LOGIN -->
 <div class="content">
-    @if(Session::get('locale') == 'zh-CN')
-        <a href="/?locale=en">English</a>
-    @else
-        <a href="/?locale=zh-CN">中文</a>
-    @endif
+    <nav style="padding-bottom: 20px;text-align: center;">
+        @if(app()->getLocale() == 'zh-CN')
+            <a style="border: 1px solid;padding: 10px;" href="{{url('lang', ['locale' => 'en'])}}">English</a>
+        @else
+            <a style="border: 1px solid;padding: 10px;" href="{{url('lang', ['locale' => 'zh-CN'])}}">中文</a>
+        @endif
+    </nav>
     <!-- BEGIN REGISTRATION FORM -->
     <form class="register-form" action="{{url('register')}}" method="post" style="display: block;">
         @if($is_register)

+ 7 - 0
resources/views/user/active.blade.php

@@ -37,6 +37,13 @@
 <!-- END LOGO -->
 <!-- BEGIN LOGIN -->
 <div class="content">
+    <nav style="padding-bottom: 20px;text-align: center;">
+        @if(app()->getLocale() == 'zh-CN')
+            <a style="border: 1px solid;padding: 10px;" href="{{url('lang', ['locale' => 'en'])}}">English</a>
+        @else
+            <a style="border: 1px solid;padding: 10px;" href="{{url('lang', ['locale' => 'zh-CN'])}}">中文</a>
+        @endif
+    </nav>
     @if(Session::get('errorMsg'))
         <div class="alert alert-danger">
             <span> {{Session::get('errorMsg')}} </span>

+ 7 - 0
resources/views/user/activeUser.blade.php

@@ -37,6 +37,13 @@
 <!-- END LOGO -->
 <!-- BEGIN LOGIN -->
 <div class="content">
+    <nav style="padding-bottom: 20px;text-align: center;">
+        @if(app()->getLocale() == 'zh-CN')
+            <a style="border: 1px solid;padding: 10px;" href="{{url('lang', ['locale' => 'en'])}}">English</a>
+        @else
+            <a style="border: 1px solid;padding: 10px;" href="{{url('lang', ['locale' => 'zh-CN'])}}">中文</a>
+        @endif
+    </nav>
     @if (Session::get('errorMsg'))
         <div class="alert alert-danger">
             <button class="close" data-close="alert"></button>

+ 12 - 0
resources/views/user/free.blade.php

@@ -45,6 +45,18 @@
             </a>
         </div>
         <!-- END LOGO -->
+        <!-- BEGIN PAGE TOP -->
+        <div class="page-top">
+            <!-- BEGIN Language TOGGLER -->
+            <div style="display:inline-table;height:100%;float:left;">
+                @if(app()->getLocale() == 'zh-CN')
+                    <a style="display:table-cell;vertical-align:middle;color:red;font-weight:600;" href="{{url('lang', ['locale' => 'en'])}}">English</a>
+                @else
+                    <a style="display:table-cell;vertical-align:middle;color:red;font-weight:600;" href="{{url('lang', ['locale' => 'zh-CN'])}}">中文</a>
+                @endif
+            </div>
+            <!-- END Language TOGGLER -->
+        </div>
     </div>
     <!-- END HEADER INNER -->
 </div>

+ 9 - 7
resources/views/user/layouts.blade.php

@@ -50,15 +50,17 @@
         <!-- BEGIN RESPONSIVE MENU TOGGLER -->
         <a href="javascript:;" class="menu-toggler responsive-toggler" data-toggle="collapse" data-target=".navbar-collapse"> </a>
         <!-- END RESPONSIVE MENU TOGGLER -->
-        <!-- BEGIN Language TOGGLER -->
-        @if(Session::get('locale') == 'zh-CN')
-            <a href="/?locale=en">English</a>
-        @else
-            <a href="/?locale=zh-CN">中文</a>
-        @endif
-        <!-- END Language TOGGLER -->
         <!-- BEGIN PAGE TOP -->
         <div class="page-top">
+            <!-- BEGIN Language TOGGLER -->
+            <div style="display:inline-table;height:100%;float:left;">
+                @if(app()->getLocale() == 'zh-CN')
+                    <a style="display:table-cell;vertical-align:middle;color:red;font-weight:600;" href="{{url('lang', ['locale' => 'en'])}}">English</a>
+                @else
+                    <a style="display:table-cell;vertical-align:middle;color:red;font-weight:600;" href="{{url('lang', ['locale' => 'zh-CN'])}}">中文</a>
+                @endif
+            </div>
+            <!-- END Language TOGGLER -->
             <!-- BEGIN TOP NAVIGATION MENU -->
             <div class="top-menu">
                 <ul class="nav navbar-nav pull-right">

+ 7 - 0
resources/views/user/reset.blade.php

@@ -37,6 +37,13 @@
 <!-- END LOGO -->
 <!-- BEGIN LOGIN -->
 <div class="content">
+    <nav style="padding-bottom: 20px;text-align: center;">
+        @if(app()->getLocale() == 'zh-CN')
+            <a style="border: 1px solid;padding: 10px;" href="{{url('lang', ['locale' => 'en'])}}">English</a>
+        @else
+            <a style="border: 1px solid;padding: 10px;" href="{{url('lang', ['locale' => 'zh-CN'])}}">中文</a>
+        @endif
+    </nav>
     <!-- BEGIN REGISTRATION FORM -->
     <form class="register-form" action="{{url(Request::getRequestUri())}}" method="post" style="display: block;">
         @if(Session::get('errorMsg'))

+ 7 - 0
resources/views/user/resetPassword.blade.php

@@ -37,6 +37,13 @@
 <!-- END LOGO -->
 <!-- BEGIN LOGIN -->
 <div class="content">
+    <nav style="padding-bottom: 20px;text-align: center;">
+        @if(app()->getLocale() == 'zh-CN')
+            <a style="border: 1px solid;padding: 10px;" href="{{url('lang', ['locale' => 'en'])}}">English</a>
+        @else
+            <a style="border: 1px solid;padding: 10px;" href="{{url('lang', ['locale' => 'zh-CN'])}}">中文</a>
+        @endif
+    </nav>
     @if (Session::get('errorMsg'))
         <div class="alert alert-danger">
             <button class="close" data-close="alert"></button>

+ 1 - 0
routes/web.php

@@ -3,6 +3,7 @@
 Route::get('s/{code}', 'SubscribeController@index'); // 节点订阅地址
 
 Route::group(['middleware' => ['forbidden']], function () {
+    Route::get('lang/{locale}', 'UserController@switchLang'); // 语言切换
     Route::any('login', 'LoginController@index'); // 登录
     Route::get('logout', 'LoginController@logout'); // 退出
     Route::any('register', 'RegisterController@index'); // 注册