Home > Back-end >  Laravel localization for custom error pages
Laravel localization for custom error pages

Time:11-17

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.

CodePudding user response:

Open app/exceptions/handler.php

find render function paste here

don't for get import this trait


use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;


public function render($request, Exception $e)
{

    if($e instanceof NotFoundHttpException)
    {
        if(\Request::hasCookie('language')) {
            $cookie = \Request::cookie('language');
            app()->setLocale($cookie);
           //.... etc
        }
    }

    return parent::render($request, $e);
}

CodePudding user response:

You can also redirect to other pages in App\Exceptions\Handler.php. You can also assign using App::setLocale(). Like this:

public function render($request, Throwable $exception)
{
    App::setLocale('en_GB');
    /** @var \Symfony\Component\HttpKernel\Throwable $e */
    $e = $exception;
    $statusCode = $e->getStatusCode();
    return $this->isHttpException($exception) && $statusCode == 404 ?
        response()->view('frontend.pages.404') :
        parent::render($request, $exception);
}
  • Related