I have a multilanguage site: en, hu, ro atm. The base URL is example.com/{locale}
e.g. example.com/en
. The language detection is just checking the locale
parameter of the route. I want it to stay this way.
I have a language selector where all locales can be selected and for every page I have link rel alternate for the crawlers to avoid duplicate content problems. So when I visit example.com/en/products/1
the language selector in the layout should have example.com/hu/products/1
and example.com/ro/products/1
as well. I think I should make these URIs in the language middleware and send it to the layout with View::share
.
I could build these URIs using the current URI and a simple preg replace or str replace on the first segment, but I guess there is a better way in Laravel. I found that IlluminateSupportFacadesRequest::segment(1)
is good for getting the first segment and the route pattern is usually /{locale}/sthg/{sthg}
appended with query string, but I cannot find a Laravel solution for replacing the first segment. Is there such a thing?
2
Answers
assuming you have a controller that handels changes to your locale:
if you want to build all language options, and pass them to your views, you can use a view composer: