Laravel 5.8 - Get translated Routes
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hello everyone, I have a Laravel 5.8 website with two domains, one for English and one for the French version.

How could I translate the current url to the other language ? For instance, I have a page http://mywebsite.test/users/1 and I have and button to switch language at the top. If I click on fr to go to the French website, it should bring me to http://monsiteweb.test/utilisateurs/1.

Sometimes their might be more parameters like http://mywebsite.test/users/1/articles/3.

my route file looks like this:
Route::domain('mywebsite.test')->group(function () {
Route::get(('routes.car-insurance', [], 'en'), 'PagesController')->name('car-insurance');
Route::get(
('routes.home-insurance', [], 'en'), 'PagesController')->name('home-insurance');
Route::get(__('routes.about', [], 'en'), 'PagesController')->name('about');
});

Route::domain('monsiteweb.test')->group(function () {
Route::get(('routes.car-insurance', [], 'fr'), 'PagesController')->name('car-insurance');
Route::get(
('routes.home-insurance', [], 'fr'), 'PagesController')->name('home-insurance');
Route::get(__('routes.about', [], 'fr'), 'PagesController')->name('about');
});

Anyone know how I could create a function that would do this for me ?

7 months ago
Tags
PHP
laravel

Crowdsource coding tasks.

1 Solution


<?php
function currentPage() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
?>

<?php
function LangDesignatedDomain($thedomain) {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
$pageURL .= $thedomain;
}
return $pageURL.= currentPage();
}
?>

Then just call the function when you click the button to append to your desired domain: LangDesignatedDomain("monsiteweb.test");

View Timeline