Skip to content

Commit a45f1ad

Browse files
committed
set locale
1 parent 683e80b commit a45f1ad

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ You will typically want to define your SEO tags dynamically in your controllers.
218218
namespace App\Http\Controllers;
219219

220220
use \Elegantly\Seo\Facades\SeoManager;
221+
use Elegantly\Seo\Standard\Alternate;
221222

222223
class HomeController extends Controller
223224
{
@@ -226,14 +227,26 @@ class HomeController extends Controller
226227
// Using the helper
227228
seo()
228229
->setTitle("Homepage")
230+
->setImage(asset('images/opengraph.jpg'))
229231
->setDescription("The homepage description")
230-
->when($condition, fn($seo) => $seo->noIndex());
232+
->when(!App::isProduction(), fn($seo) => $seo->noIndex())
233+
->setLocale("fr")
234+
->setAlternates([
235+
new Alternate(
236+
hreflang: "en",
237+
href: route('home', ['locale' => "en"]),
238+
),
239+
new Alternate(
240+
hreflang: "fr",
241+
href: route('home', ['locale' => "fr"]),
242+
),
243+
]);
231244

232245
// Using the facade
233246
SeoManager::current()
234247
->setTitle("Homepage")
235-
->setDescription("The homepage description")
236-
->when($condition, fn($seo) => $seo->noIndex());
248+
->setDescription("The homepage description");
249+
// ...
237250

238251
return view('home');
239252
}

src/SeoManager.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Elegantly\Seo;
44

55
use Elegantly\Seo\Contracts\Taggable;
6+
use Elegantly\Seo\OpenGraph\Locale;
67
use Elegantly\Seo\OpenGraph\OpenGraph;
78
use Elegantly\Seo\Schemas\Schema;
89
use Elegantly\Seo\Schemas\WebPage;
@@ -11,6 +12,7 @@
1112
use Elegantly\Seo\Twitter\Cards\Card;
1213
use Elegantly\Seo\Twitter\Cards\Summary;
1314
use Illuminate\Contracts\Support\Htmlable;
15+
use Illuminate\Support\Facades\App;
1416
use Illuminate\Support\Traits\Conditionable;
1517
use Stringable;
1618

@@ -160,6 +162,22 @@ public function setSitmap(?string $value): static
160162
return $this;
161163
}
162164

165+
/**
166+
* @return $this
167+
*/
168+
public function setLocale(
169+
string $value,
170+
): static {
171+
if ($this->opengraph) {
172+
$this->opengraph->locale = new Locale(
173+
locale: $value,
174+
alternate: $this->opengraph->locale?->alternate ?? [],
175+
);
176+
}
177+
178+
return $this;
179+
}
180+
163181
/**
164182
* @param null|Alternate[] $value
165183
* @return $this
@@ -170,6 +188,13 @@ public function setAlternates(?array $value): static
170188
$this->standard->alternates = $value;
171189
}
172190

191+
if ($this->opengraph) {
192+
$this->opengraph->locale = new Locale(
193+
locale: $this->opengraph->locale?->locale ?? App::getLocale(),
194+
alternate: array_map(fn ($item) => $item->toOpenGraph(), $value ?? [])
195+
);
196+
}
197+
173198
return $this;
174199
}
175200

src/Standard/Alternate.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public function __construct(
1818
//
1919
}
2020

21+
public function toOpenGraph(): string
22+
{
23+
return $this->hreflang;
24+
}
25+
2126
public function toTags(): SeoTags
2227
{
2328
return new SeoTags([

0 commit comments

Comments
 (0)