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!