skip to Main Content

lets assume i have a website
www.example.com
and admin dashboard:
www.example.com/admin/*
i need to force locale language for all routes of admin/ to english
all admin routes located in routes/admin.php
what i have already tried:

Route::group(['middleware' => 'auth:admin'], function() {

    App::setLocale( 'en');//**not working**
    app()->setLocale('en');//**not working**
    //other routes of admin

});

and tried this and also not working: in the app/http/kernel.php

protected $middlewareGroups = [
   
    'admin' => [
        AppHttpMiddlewareLocalizationAdmin::class,
    ],
];

where the LocalizationAdmin class is:

<?php

namespace AppHttpMiddleware;

use Closure;

class LocalizationAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $locale='en';

        app()->setLocale($locale);

        return $next($request);
    }
}

2

Answers


  1. Edit your middleware function like this, you can set your app locale for all your routes that are admin in the first segment:

    public function handle($request, Closure $next)
    {
        if ($request->segment(1) == 'admin') {
            app()->setLocale('en');
        }
        return $next($request);
    }
    

    Remember to make sure you use the middleware for your related routes.

    Login or Signup to reply.
  2. First create new middleware

    php artisan make:middleware AdminLocale
    

    In the middleware

    public function handle($request, Closure $next)
    {
        app()->setLocale('en');
        return $next($request);
    }
    

    Register it $routeMiddleware (path app/Http/Kernel.php)

    'adminLocale' => AppHttpMiddlewareAdminLocale::class,
    

    In the route group call

    Route::group(['middleware' => ['auth', 'AdminLocale']], function () {
        // Admin routes
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search