Skip to content

Commit

Permalink
feat: Set alternate language(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
DevDavido committed Feb 10, 2024
1 parent e72c123 commit 8a27e8d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/SEOTools/Contracts/MetaTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,25 @@ public function addAlternateLanguage($lang, $url);
*/
public function addAlternateLanguages(array $languages);

/**
* Set an alternate language.
*
* @param string $lang language code in format ISO 639-1
* @param string $url
*
* @return static
*/
public function setAlternateLanguage($lang, $url);

/**
* Set alternate languages.
*
* @param array $languages
*
* @return static
*/
public function setAlternateLanguages(array $languages);

/**
* Get the title formatted for display.
*
Expand Down
2 changes: 2 additions & 0 deletions src/SEOTools/Facades/SEOMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
* @method static \Artesaos\SEOTools\Contracts\MetaTags setNext(string $url)
* @method static \Artesaos\SEOTools\Contracts\MetaTags addAlternateLanguage(string $lang, string $url)
* @method static \Artesaos\SEOTools\Contracts\MetaTags addAlternateLanguages(array $languages)
* @method static \Artesaos\SEOTools\Contracts\MetaTags setAlternateLanguage(string $lang, string $url)
* @method static \Artesaos\SEOTools\Contracts\MetaTags setAlternateLanguages(array $languages)
* @method static string getTitle()
* @method static string getTitleSession()
* @method static string getTitleSeparator()
Expand Down
30 changes: 29 additions & 1 deletion src/SEOTools/SEOMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ public function generate($minify = false)
}

foreach ($languages as $lang) {
$html[] = "<link rel=\"alternate\" hreflang=\"{$lang['lang']}\" href=\"{$lang['url']}\">";
if (!empty($lang['lang'] && !empty($lang['url']))) {
$html[] = "<link rel=\"alternate\" hreflang=\"{$lang['lang']}\" href=\"{$lang['url']}\">";
}
}

if ($robots) {
Expand Down Expand Up @@ -387,6 +389,32 @@ public function addAlternateLanguages(array $languages)
return $this;
}

/**
* {@inheritdoc}
*/
public function setAlternateLanguage($lang, $url)
{
// Remove language if already existing
$this->alternateLanguages = array_filter($this->alternateLanguages, function ($arr) use ($lang) {
return $arr['lang'] !== $lang;
});

// Append (updated) language
$this->alternateLanguages[] = ['lang' => $lang, 'url' => $url];

return $this;
}

/**
* {@inheritdoc}
*/
public function setAlternateLanguages(array $languages)
{
$this->alternateLanguages = $languages;

return $this;
}

/**
* Sets the meta robots.
*
Expand Down

0 comments on commit 8a27e8d

Please sign in to comment.