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

Shiva's avatar

Laravel localization

Hi, I'm busy learning laravel's localization and I was wondering is there a way to have the language change when the url looks like this

home.fr

instead of the url looking like this

home/fr/

In my routes.php

Route::group(['prefix' => '{lang?}', 'before' => 'localization'], function() {
    Route::get('/', function() {
        return View::make('localization_test');
    });
});

my filters.php

Route::filter('localization', function() {
    App::setLocale(Route::input('lang'));
});

in my lang/en/localization_test.php

return array(
    'title' => 'English title',
    'subtitle' => 'English subtitle',
);

in my lang/fr/localization_test.php

return array(
    'title' => 'French title',
    'subtitle' => 'French subtitle',
);

in my views/localization.blade.php

<h1>{{ trans('localization_test.title') }}</h1>
<h2>{{ trans('localization_test.subtitle') }}</h2>
0 likes
5 replies
bart's avatar

You just have to implement a TLD check method to you localization filter using Request::root() as second parameter for a preg_match() and then set the locale accordingly.

App::setLocale($detectedTld);

Shiva's avatar

I created a helper function to get the TLD

helpers.php

if ( ! function_exists('get_tld'))
{
    function get_tld()
    {
        $matches = array();
        preg_match('/laravel-local(.[a-zA-Z]+)/', Request::root(), $matches);
        $tld = $matches[1];
        return $tld;
    }
}

and I made changes to my filters.php and my routes.php

My filters.php

Route::filter('localization', function() {
    $tld = get_tld();
    App::setLocale($tld);
});

My routes.php

Route::group(['suffix' => '{$tld}', 'before' => 'localization'], function() {
    Route::get('/', function() {
        return View::make('localization_test');
    });
});

Now if I change my url to include the .fr where the .com is I get this error

Not Found
The requested URL /laravel-local.fr/public/ was not found on this server.

but what is suppose to happen is when my url has .com then it displays

English title
English subtitle

and that is working, but when I put a .fr in my url it's suppose to show

French title
French subtitle
papa's avatar

laravel detector works for domains home.fr, home.gr, home.de ???

Please or to participate in this conversation.