Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andy's avatar
Level 8

mcamara/laravel-localization : usage and understanding -- need help

I'm trying to accomplish:

  • no locale in url for the local set in config.php
  • when a language is selected the local is set in the url

What I have in code:

kernel.php

    protected $routeMiddleware = [
// Locale REDIRECTION MIDDLEWARE
        'localize'                  => 'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes',
        'localizationRedirect'      => 'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter',
        'localeSessionRedirect'     => 'Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect'

routes.php

Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
//------- ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP -------//

Route::get('/', 'HomeController@index');
Route::get('home', 'HomeController@index');

Route::get('welcome', 'WelcomeController@index');

});

config/laravellocalization.php

    'useAcceptLanguageHeader' => true,
    'hideDefaultLocaleInURL' => true,

What is happening: The language selector uses code as per readme and will switch the url to "es/home" and will translate the page correctly. When I click on the link "/home" the language returns to English.

What am I doing wrong? Do I need to code or set my urls a certain way? If so would you mind providing some code?

I did look at the blog app that mcamara has in his repositories but it sets all urls as "en/home" or "es/home" which isn't really what I am trying to do so I didn't download and install to use as a learning source.

Thanks!

0 likes
7 replies
andy's avatar
Level 8

Is there a way of having links not use something like this?

            <li><a href="/{{ LaravelLocalization::getCurrentLocale() }}">{{ trans('kotoba::general.home') }}</a></li>
rjsworking's avatar

Hi. I guess "en" is the app default locale. Change it in config/app.php to the locale you want. The default locale won't be in the url because you have 'hideDefaultLocaleInURL' => true. You don't need to have links the way you show. Use the url() helper with your routes.

Cheers

andy's avatar
Level 8

So, the link does not need to have "en" or "es" in the link?

Currently, I'm using this:

Route::group(
[
    'prefix' => LaravelLocalization::setLocale(),
    'middleware' => [ 'localizationRedirect', 'localeSessionRedirect' ]
],
function()
{

Sorry I should have updated my post.

alrocha's avatar
alrocha
Best Answer
Level 2

I had the same problem, just delete the 'localizationRedirect' and magically worked!

1 like
andy's avatar
Level 8

@alrocha

Mucho Gracias!

Thanks! that did it. I actually recoded the LocaleSessionRedirect but I still was having to need to change my routes to include the getCurrentLocale(). Removing localizationRedirect from my route file solved all that.

Thanks!!

alrocha's avatar

Hahaha de nada amigo!

Also I had a problem when I was trying to store or update a new object, and I realize that I must put that resource route out of the group for localization as well.

into1's avatar

Hey guys,

please help. Same problem as op here. Even tried to remove 'localizationRedirect', but it still (doesn't)work the same way. I appreciate any help!

Please or to participate in this conversation.