Skip to content

Commit

Permalink
Add alternate for multi-lang sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
flody committed Oct 7, 2013
1 parent 75fd07b commit 7773a61
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
36 changes: 36 additions & 0 deletions classes/Kohana/Sitemap/URL.php
Expand Up @@ -9,6 +9,7 @@ abstract class Kohana_Sitemap_URL implements Kohana_Sitemap_Interface
'changefreq' => NULL,
'priority' => NULL,
);
private $alternates = array();

/**
* URL of the page. This URL must begin with the protocol (such as http) and end
Expand Down Expand Up @@ -92,6 +93,31 @@ public function set_priority($priority)

return $this;
}
/**
* Submit rel-alternate-hreflang annotations in a Sitemap
* @see https://support.google.com/webmasters/answer/2620865?hl=en
* @param string $lang
* @param string $location
*/
public function set_alternate($lang,$location)
{
if ( ! Valid::max_length($location, 2048))
{
throw new LengthException('The location was too long, maximum length of 2,048 characters.');
}

$location = Sitemap::encode($location);

if ( ! Valid::url($location))
{
throw new InvalidArgumentException('The location was not a valid URL');
}

$alternate = array('lang' => $lang,'location' => $location);
$this->alternates[] = $alternate;

return $this;
}

/**
* @var Kohana_Sitemap_Interface
Expand Down Expand Up @@ -130,6 +156,16 @@ public function create()
$url_node->appendChild(new DOMElement($name, $value));
}
}
if ($this->alternates != NULL)
{
foreach($this->alternates as $k => $alternate)
{
$xhtmllink = $url_node->appendChild(new DOMElement('link'));
$xhtmllink->setAttributeNode(new DOMAttr('rel', 'alternate'));
$xhtmllink->setAttributeNode(new DOMAttr('hreflang', $alternate['lang']));
$xhtmllink->setAttributeNode(new DOMAttr('href', $alternate['location']));
}
}

// If a specialised sitemap was used, import it's data here.
if (NULL !== $this->driver)
Expand Down
32 changes: 30 additions & 2 deletions guide/sitemap/sitemap.basic.md
Expand Up @@ -41,9 +41,37 @@ The above example should output the following
# Requirements

| Argument | Requirements |
|------------------|-----------------------------------------------------------------------------------------------------------------|
|------------------------------------------------------------------------------------------------------------------------------------|
| loc | Maximum length of 2,048 characters. |
| | Must pass Validate::url |
| last mod | Unixtime stamp |
| change frequency | Must be one of the following: **always**, **hourly**, **daily**, **weekly**, **monthly**, **yearly**, **never** |
| priority | Must be a numeric value between **0 (zero)** and **1 (one)** and is **inclusive**. |
| priority | Must be a numeric value between **0 (zero)** and **1 (one)** and is **inclusive**. |
| alternate | lang , location (see loc) |


# Alternate

You can add alternate for multi-lingual page. Ex:

$url->set_alternate('fr-fr','http://google.fr');

Output:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://google.com</loc>
<lastmod>2010-06-17T19:48:12+01:00</lastmod>
<changefreq>daily</changefreq>
<priority>1</priority>
<link rel="alternate" hreflang="fr-fr" href="http://google.fr"/>
</url>
</urlset>

Don't forget to add a set_alternate for the current page:

$url->set_alternate('en','http://google.com');

As said here:

https://support.google.com/webmasters/answer/2620865?hl=en

0 comments on commit 7773a61

Please sign in to comment.