Irakli Qiria
Irakli Qiria

Reputation: 11

Localization with Laravel

I have a multilingual website and I need to get language symbol in url e.g. example.com/en/blogs and I do it with this way web.php ->

Route::prefix('{locale}')
    ->middleware(Localization::class)
    ->group(function () {

        Route::get('/', function () {
            return view('welcome');
        })->name('home');
        
        Route::get('blog', [App\Http\Controllers\BlogController::class, 'index'])->name('blog.index');})

middleware

public function handle(Request $request, Closure $next): Response
{  
    app()->setLocale($request->segment(1));
    URL::defaults(['locale' => $request->segment(1)]);

    return $next($request);
}

This way works, When I try change language from url it works e.g. if I change en to ka it works but I have issue with language switcher button. I searched and find this way for doing switcher button:

<a href="{{ route(Route::currentRouteName(), 'en') }}" class="mx-[2px] hover:text-primary duration-300 {{ app()->getLocale() === "en" ? 'text-primary font-bold' : '' }}">EN <span class="fi fi-gb ml-1"></span></a>
<a href="{{ route(Route::currentRouteName(), 'ka') }}" class="mx-[2px] hover:text-primary duration-300 {{ app()->getLocale() === "ka" ? 'text-primary font-bold' : '' }}">KA<span class="fi fi-ka ml-1"></span></a>

But when I click the buttons it redirect to this url -> example.com/en/blogs?ka or example.com/en/blogs?en

I need change prefix with localization

Upvotes: 0

Views: 51

Answers (1)

Rayzel
Rayzel

Reputation: 102

<a href="{{ route(Route::currentRouteName(), ['locale' => 'en']) }}" class="mx-[2px] hover:text-primary duration-300 {{ app()->getLocale() === "en" ? 'text-primary font-bold' : '' }}">EN <span class="fi fi-gb ml-1"></span></a>
<a href="{{ route(Route::currentRouteName(), ['locale' => 'ka']) }}" class="mx-[2px] hover:text-primary duration-300 {{ app()->getLocale() === "ka" ? 'text-primary font-bold' : '' }}">KA<span class="fi fi-ka ml-1"></span></a>

you c=should change the value of the prefix

Upvotes: 0

Related Questions