0

我之前在另一个项目中做过这个本地化,但是这个项目我在管理中间件中进行了更改,以便为仪表板中的用户授予权限。该项目适用于阿拉伯语和英语两种语言,它在网站管理员中运行良好,但在管理员中我不知道我的代码中有什么问题。

现在本地化不起作用

这是我的路线:

Route::namespace('admin')->group(function (){
   Route::group(['middleware'=> ['web','admin']], function (){
       Route::prefix('admin-123')->group(function (){

           Route::get('locale/{locale}', function ($locale){
               \Illuminate\Support\Facades\Session::put('locale', $locale);
               return redirect()->back();
           });

这是我的中间件:

<?php

namespace App\Http\Middleware;

use App\user_permission_group;
use Closure;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use \Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Auth;

class admin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(!auth()->check()){
            return redirect('login');
        }

        if(Auth::user()->admin > 2)
        {
            return redirect('/');
        }

        $url = Request::fullUrl();

        $lang = explode('admin-123/locale/', $url);
//        dd($lang[1]);
        if (isset($lang[1]) && $lang[1] !='')
        {
            App::setLocale($lang[1]);
//            Session::put('locale', $lang[1]);
        }

        $admin = explode('admin-123', $url);
        $permission = explode('/', $admin[1]);

        if (isset($permission[1]) && $permission[1] !='' && $permission[1] != 'locale')
        {
            if (Request::is('admin-123/' . $permission[1] .'/*')) {
                $permissions = DB::table('permission_group_details')->select('group_id')->where('permission', $permission[1])->get();
                if ($permissions->count() > 0)
                {
                    foreach ($permissions as $per)
                    {
                        $gr_id[] = $per->group_id;
                    }
                    $grid = collect($gr_id);
                    $users = user_permission_group::where('user_id', Auth::user()->id)->whereIn('group_id', $grid)->first();

                    if (!isset($users)){ return redirect()->back(); }

                    $permission_route = DB::table('permission_group_details')->select('permission')->where('group_id' , $users->group_id)->first();
                    if (!isset($permission_route) && $permission_route =='')
                    {
                        return redirect()->back();
                    }
                }
                else{
                    return redirect()->back();
                }
            }
        }

        return $next($request);
    }
}

这是我的刀片:

@if(\Illuminate\Support\Facades\App::isLocale('en'))
        @php
            $lang = 'English';
            $pull_right = '';
            $pull_left = 'pull-right';
            $rtl ='';
            $title = 'title';
            $currency = 'EGP';
            $pull = 'pull-left';
        @endphp
    @else
        @php
            $lang = 'العربية';
            $pull_right = 'pull-right';
            $pull_left = 'pull-left';
            $rtl ='rtl';
            $title = 'title_ar';
            $currency = 'جنيه';
            $pull = 'pull-right';
        @endphp
    @endif

        
    <li class="dropdown dropdown-small">
       <a href="#" class="dropdown-toggle" data-hover="dropdown" data-toggle="dropdown"><span class="value">{{ $lang }} </span><b class="caret"></b></a>
          <ul class="dropdown-menu" style="padding: 0.5rem 1.5rem !important; margin: 0.125rem 5px 0 !important;">
               <li><a href="{{ url('admin-123/locale/en') }}">English</a></li>
               <li><a href="{{ url('admin-123/locale/ar') }}">العربية</a></li>
           </ul>
    </li>

请帮忙..谢谢大家

4

1 回答 1

1

您需要添加中间件语言并编写以下代码:

<?php

namespace App\Http\Middleware;

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

class language
{
    public function handle($request, Closure $next)
    {
        $raw_locale = $request->session()->get('locale');
        if (in_array($raw_locale, Config::get('app.locales'))) {
            $locale = $raw_locale;
        }
        else $locale = Config::get('app.locale');
        App::setLocale($locale);
        return $next($request);
    }
}

并在内核中添加这一行

'language' => \App\Http\Middleware\language::class,

并添加 config.app

 'locale' => 'ar',
    'locales' => ['ar', 'en'],

并在路线:

<li><a href="{{ route('language.change','en') }}">English</a></li>
                                <li><a href="{{ route('language.change','ar') }}">العربية</a></li>

并在路线中

Route::group(['middleware' => ['admin', 'language']], function () {
            Route::get('setlocale/{locale}', function ($locale) {
                if (in_array($locale, \Config::get('app.locales'))) { session(['locale' => $locale]); }
                return redirect()->back();
            })->name('language.change');
});
于 2020-07-26T20:48:25.247 回答