i made this localization before in another project , but this project i changed in admin middleware to make permissions for users in dashboard. this project is for two languages arabic and english it is working fine in website out admin but in admin i don't know what is the problem in my code.
now localization not working
this is my route:
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();
});
and this is my middleware:
<?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);
}
}
and this is my blade :
@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>
please help.. thanks for all
you need to add middleware language and write this code:
<?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);
}
}
and in kernel add this row
'language' => \App\Http\Middleware\language::class,
and add in config.app
'locale' => 'ar',
'locales' => ['ar', 'en'],
and in route:
<li><a href="{{ route('language.change','en') }}">English</a></li>
<li><a href="{{ route('language.change','ar') }}">العربية</a></li>
and in route
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');
});