1

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

My site is structured like this:

http://example.com/{locale}

<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>

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();
    }
}
IMSoP
  • 89,526
  • 13
  • 117
  • 169
jhon-jhones
  • 455
  • 1
  • 13
  • 27

1 Answers1

0

In your app, you have access to the current locale via App::getLocale().

So I think you could use

hreflang="{{ App::getLocale() }}"

to set the hreflang attribute of your links to the current locale.

Shushiro
  • 577
  • 1
  • 9
  • 32
  • maybe I got your question wrong - sorry >.< do you want to set the locale of the language in each link? I wonder, shouldn't the hreflang attribute always have the language in which the site is currently displayed? – Shushiro Feb 25 '21 at 11:31
  • if you want to set the locale for the link leading to french site as "fr", I think you could hardcode it? (as example and do it for all languages) – Shushiro Feb 25 '21 at 11:43