I am looking for a way for an entire application route to have multiple locales without using route groups. This is because I use a package of external extensions, which means that routes are registered in many places.
Essentially, I want to have / foo / bar, as well as / en / foo / bar, / de / foor / bar, / es / foo / bar, etc. everything to be recognized and processed along the route / foot / bar
Route::get('foo/bar', function () { return App::getLocale() . ' result'; });
So the above will give me the result “result” or “result” or “result”.
I already have middleware that sets the locale based on the path segment. I tried the following with no luck.
... $newPath = str_replace($locale,'',$request->path()); $request->server->set('REQUEST_URI',$new_path); } return $next($request);
I hope this is possible, or there is another way to achieve it.
EDIT ------
Based on the comment below, I quickly cracked it by adding the following code to public / index.php. Hope this gives a better idea of what I'm trying to achieve by editing the request object.
$application_url_segments = explode( '/', trim( $_SERVER["REQUEST_URI"], '/' ) ); $application_locale = $application_url_segments[0]; $application_locales = ['en' => 'English', 'de' => 'German']; if ( array_key_exists( $application_locale, $application_locales ) ) { $_SERVER["REQUEST_URI"] = str_replace( '/' . $application_locale,'',$_SERVER["REQUEST_URI"] ); }