-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1623 from DmitriySmolin/main
add component for language markup
- Loading branch information
Showing
4 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters