Skip to content

Commit

Permalink
Merge pull request #1623 from DmitriySmolin/main
Browse files Browse the repository at this point in the history
add component for language markup
  • Loading branch information
fey authored May 23, 2024
2 parents cacafdc + 97e69f1 commit ce527f6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ public function boot(): void
Blade::include('components.comments', 'comments');
Blade::include('components.solutions', 'solutions');
Blade::include("components.solution", 'solution');
Blade::include("components.hreflang_tags", 'hreflang_tags');
}
}
48 changes: 48 additions & 0 deletions app/View/Components/HreflangTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\View\Components;

use Illuminate\View\Component;
use Illuminate\View\View;

class HreflangTags extends Component
{
public array $languageUrls = [];
public string $currentLocale;

public function __construct()
{
$this->generateLanguageUrls();
}

private function generateLanguageUrls(): void
{
$defaultLocale = config('app.locale');
$this->currentLocale = app()->getLocale();
$segments = request()->segments();

$url = implode('/', $segments);

if ($this->currentLocale !== $defaultLocale) {
$url = "$this->currentLocale/$url";
}

if ($this->currentLocale === 'ru') {
$this->languageUrls['en'] = $this->removeLanguagePrefixes($url);
} else {
$this->languageUrls['ru'] = "ru/$url";
}

$this->languageUrls['x-default'] = $this->removeLanguagePrefixes($url);
}

public function removeLanguagePrefixes(string $url): string
{
return preg_replace('/(^\/ru\/?|\/ru\/?|\/?ru\/?|\/$)/u', '', $url);
}

public function render(): View
{
return view('components.hreflang_tags', ['languageUrls' => $this->languageUrls, 'currentLocale' => $this->currentLocale]);
}
}
3 changes: 3 additions & 0 deletions resources/views/components/hreflang_tags.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@foreach ($languageUrls as $language => $url)
<link rel="alternate" hreflang="{{ $language }}" href="{{ url('/') }}/{{ $url }}"/>
@endforeach
1 change: 1 addition & 0 deletions resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<link href="{{ mix('css/app.css') }}" rel="stylesheet">
@includeWhen(app()->environment('production'), 'layouts.deps._gtm_body')
@includeWhen(app()->environment('production'), 'layouts.deps._metrika')
<x-hreflang-tags/>
</head>

<body class="min-vh-100 d-flex flex-column">
Expand Down

0 comments on commit ce527f6

Please sign in to comment.