Skip to content

Commit

Permalink
feat: Allow to set alternate language(s) (#318)
Browse files Browse the repository at this point in the history
* test: Set alternate language(s)

* feat: Set alternate language(s)

* doc: Set alternate language(s)
  • Loading branch information
DevDavido authored May 20, 2024
1 parent 09d0524 commit 44920c7
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ SEOMeta::addKeyword($keyword);
SEOMeta::addMeta($meta, $value = null, $name = 'name');
SEOMeta::addAlternateLanguage($lang, $url);
SEOMeta::addAlternateLanguages(array $languages);
SEOMeta::setAlternateLanguage($lang, $url);
SEOMeta::setAlternateLanguages(array $languages);
SEOMeta::setTitleSeparator($separator);
SEOMeta::setTitle($title);
SEOMeta::setTitleDefault($default);
Expand Down
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
66 changes: 65 additions & 1 deletion tests/SEOTools/SEOMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function test_set_prev()
$this->assertEquals($prev, $this->seoMeta->getPrev());
}

public function test_set_alternate_languages()
public function test_add_alternate_languages()
{
$fullHeader = "<title>It's Over 9000!</title>";
$fullHeader .= "<meta name=\"description\" content=\"For those who helped create the Genki Dama\">";
Expand All @@ -270,6 +270,70 @@ public function test_set_alternate_languages()
$this->assertEquals(array_merge($expectedLangs, $expectedLangs), $this->seoMeta->getAlternateLanguages());
}

public function test_set_alternate_languages()
{
$fullHeader = "<title>It's Over 9000!</title>";
$fullHeader .= "<meta name=\"description\" content=\"For those who helped create the Genki Dama\">";
$fullHeader .= "<link rel=\"alternate\" hreflang=\"en\" href=\"http://domain.com\">";
$lang = 'en';
$langUrl = 'http://domain.com';

$expectedLangs = [['lang' => $lang, 'url' => $langUrl]];
$this->seoMeta->setAlternateLanguage($lang, $langUrl);

$this->setRightAssertion($fullHeader);
$this->assertEquals($expectedLangs, $this->seoMeta->getAlternateLanguages());

$this->seoMeta->setAlternateLanguages($expectedLangs);

$this->assertEquals($expectedLangs, $this->seoMeta->getAlternateLanguages());
}

public function test_set_override_alternate_language()
{
$fullHeader = "<title>It's Over 9000!</title>";
$fullHeader .= "<meta name=\"description\" content=\"For those who helped create the Genki Dama\">";
$fullHeader .= "<link rel=\"alternate\" hreflang=\"en\" href=\"http://domain.test\">";
$lang = 'en';
$langUrl = 'http://domain.com';
$langUrlOverridden = 'http://domain.test';

$expectedLangs = [['lang' => $lang, 'url' => $langUrl]];
$expectedLangsOverridden = [['lang' => $lang, 'url' => $langUrlOverridden]];

$this->seoMeta->setAlternateLanguage($lang, $langUrl);
$this->assertEquals($expectedLangs, $this->seoMeta->getAlternateLanguages());

$this->seoMeta->setAlternateLanguage($lang, $langUrlOverridden);
$this->setRightAssertion($fullHeader);
$this->assertEquals($expectedLangsOverridden, $this->seoMeta->getAlternateLanguages());
}

public function test_remove_alternate_language()
{
$fullHeader = "<title>It's Over 9000!</title>";
$fullHeader .= "<meta name=\"description\" content=\"For those who helped create the Genki Dama\">";
$lang = 'en';
$langUrl = 'http://domain.com';

$expectedLangs = [['lang' => $lang, 'url' => false]];

$this->seoMeta->setAlternateLanguage($lang, $langUrl);
$this->seoMeta->setAlternateLanguage($lang, false);
$this->setRightAssertion($fullHeader);
$this->assertEquals($expectedLangs, $this->seoMeta->getAlternateLanguages());
}

public function test_remove_alternate_languages()
{
$fullHeader = "<title>It's Over 9000!</title>";
$fullHeader .= "<meta name=\"description\" content=\"For those who helped create the Genki Dama\">";

$this->seoMeta->setAlternateLanguages([]);
$this->setRightAssertion($fullHeader);
$this->assertEquals([], $this->seoMeta->getAlternateLanguages());
}

public function test_set_reset()
{
$expected = "<title>It's Over 9000!</title>";
Expand Down

0 comments on commit 44920c7

Please sign in to comment.