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

lwvvliet's avatar

How to get url for the same named route in different languages?

Hi,

I'm working on a Laravel multi-domain application with separate route files for each language. Each route file has the same named routes, only the url translation is different.

Example of route named 'bike.index'

// Route file for English -> web.en.php
Route::get('/bike', 'BikeController@index')->name('bike.index');

// Route file for German -> web.de.php
Route::get('/fahrrad', 'BikeController@index')->name('bike.index');

// Route file for French -> web.fr.php
Route::get('/velo', 'BikeController@index')->name('bike.index');

// Route file for Dutch -> web.nl.php
Route::get('/fiets', 'BikeController@index')->name('bike.index');

On each page of the website I want to provide links with alternate hreflang attributes to the same page on each other domain. Like this:

<link rel="alternate" hreflang="en-gb" href="//www.example.co.uk{{ $uri_en }}" />
<link rel="alternate" hreflang="de" href="//www.example.de{{ $uri_de }}" />
<link rel="alternate" hreflang="fr" href="//www.example.fr{{ $uri_fr }}" />
<link rel="alternate" hreflang="nl" href="//www.example.nl{{ $uri_nl }}" />
<link rel="alternate" hreflang="x-default" href="//www.example.com{{ $uri_en }}" />

How do I get the correct translations for the $uri variables? I build a function that can temporary change the app locale but this doesn't give me the url in the new language. I kept getting the same language for every call to getTranslatedUri().

public static function getTranslatedUri($request, $locale)
{
    $routeName = $request->route()->getName();

        // temporary change locale to get translated uri
        \App::setLocale($locale);

        $url = route($routeName);
        // set locale back to normal
        \App::setLocale(config('site.locale'));

        return $url;
}

Any ideas? Thanks!

0 likes
2 replies
aurawindsurfing's avatar
 Route::transGet('routes.public', 'PagesController@public')->name('public');

The packege I'm using is: "arcanedev/localization": "^3.0",

All your translations sit in translation files, you have ony one route that is being resolved based on current app locale.

Hope it helps!

lwvvliet's avatar

I ended up doing something else. Now I'm generating lookup arrays for each language in advance. (I'm using a deployment hook to make sure these files are available and up-to-date)

// generate file for looking up uri by route name
$route_lookup = [];
foreach ($routes as $route) {
    $routeName = $route->getName();
        // skip routes that contain parameters for now
        // @TODO : make this smarter and dynamically include parameters?
        $pos = strpos($route->uri, "{");
            if ($pos === false) {
                    $route_lookup[$routeName] = $route->uri;
                }
        }
        file_put_contents(app_path() . "/../bootstrap/cache/routelookup." . $lang . ".php", json_encode($route_lookup));

The function to use this now looks like this:

public static function getTranslatedUri($request, $lang)
{
    try {
            // get route name
            $routeName = $request->route()->getName();

                // get this merchants route uri's
                $route_lookup = json_decode(file_get_contents(app_path() . "/../bootstrap/cache/routelookup." . $lang . ".php"), true);

                // return translated uri
                if (!empty($route_lookup[$routeName])) {
                    return \trim($route_lookup[$routeName], "/");
                }

                return "";
    } catch (\Exception $e) {
            return "";
        }
}

The only thing is that the route parameters are not taken care of yet. For now I didn't need these routes yet so left that as a todo.

Please or to participate in this conversation.