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

devhoussam123's avatar

how to set the language prefix in hreflang tag

I'm working on an international website with 3 languages (Eng - FR and AR) based on laravel localization. And I want to set the language prefix in hreflang tag.

My site is structured like this:

http://example.com/{locale}
<ul>
    <a rel="alternate" hreflang="LANGUAGE PREFIX HERE..." href="{{ url('lang/en') }}">English</a>
	<a rel="alternate" hreflang="LANGUAGE PREFIX HERE..." href="{{ url('lang/fr') }}">French</a>
	<a rel="alternate" hreflang="LANGUAGE PREFIX HERE..." href="{{ url('lang/ar') }}">Arabic</a>
</ul>

routes/web.php

Route::get('lang/{locale}', [LocalizationController::class, 'lang']);

LocalizationController

class LocalizationController extends Controller
{
    public function lang($locale) {
		App::setlocale($locale);
		session()->put('locale', $locale);
		return redirect()->back();
	}
}
0 likes
2 replies
piljac1's avatar

What are you trying to achieve exactly? Can't you simply hardcode it like you hardcoded your 3 links?

devhoussam123's avatar

I just want to generate the language prefix for hreflang tags based on laravel localization like this example :

<ul>
    <a href="{{ url('lang/en') }}" hreflang="LANGUAGE PREFIX">English</a>
	<a href="{{ url('lang/fr') }}" hreflang="LANGUAGE PREFIX">French</a>
	<a href="{{ url('lang/ar') }}" hreflang="LANGUAGE PREFIX">Arabic</a>
</ul>

English = En

French = FR

Arabic = AR

Please or to participate in this conversation.