skip to Main Content

I have custom error pages e.g. resources/views/errors/404.blade.php everything is working just fine but the localization is not working for error pages. If I change website language the error pages still show in default language I tried in many way but its not working, Can anyone please help me make this work thanks in advance.

I try to make it work via exception handler but don’t know how to do that. I can apply language middleware is someone can tell me where is default routes for error pages.

3

Answers


  1. Chosen as BEST ANSWER

    thank you guys a little discussion with you guys fixed my problem I get the session's locale value in exception handler that worked for me I am answering it may be can help some one else. Below are the thing I did in App/Exception/Handler.php

    use Session;
    
    public function render($request, Throwable $exception)
    {
        app()->setLocale(Session::get('locale'));
        return parent::render($request, $exception);
    }
    

    also i moved

    IlluminateSessionMiddlewareStartSession::class,
    

    from web group to global group in kernal.php


  2. Open app/exceptions/handler.php

    find render function paste here

    don’t for get import this trait

    
    use SymfonyComponentHttpKernelExceptionNotFoundHttpException;
    
    
    
    public function render($request, Exception $e)
    {
    
      if($request->hasCookie('language')) {
            // Get cookie
        $cookie = $request->cookie('language');
            // Check if cookie is already decrypted if not decrypt
        $cookie = strlen($cookie) > 2 ? decrypt($cookie) : $cookie;
            // Set locale
        app()->setLocale($cookie);
        }
    
        if($e instanceof NotFoundHttpException) {
            return response()->view('errors.404', [], 404);
        }
    
        return parent::render($request, $e);
    }
    
    Login or Signup to reply.
  3. You can also redirect to other pages in AppExceptionsHandler.php. You can also assign using App::setLocale(). Like this:

    public function render($request, Throwable $exception)
    {
        App::setLocale('en_GB');
        /** @var SymfonyComponentHttpKernelThrowable $e */
        $e = $exception;
        $statusCode = $e->getStatusCode();
        return $this->isHttpException($exception) && $statusCode == 404 ?
            response()->view('frontend.pages.404') :
            parent::render($request, $exception);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search