Skip to content

Instantly share code, notes, and snippets.

@wilr
Created November 8, 2014 19:37
Show Gist options
  • Save wilr/0be1d5fa228c6ee18c18 to your computer and use it in GitHub Desktop.
Save wilr/0be1d5fa228c6ee18c18 to your computer and use it in GitHub Desktop.
Implement localisation for Google Sitemaps
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='{$BaseHref}googlesitemaps/templates/xml-sitemap.xsl'?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<% loop $Items %>
<url>
<loc>$AbsoluteLink</loc><% if Localisations %><% loop Localisations %>
<xhtml:link rel="alternate" hreflang="$PlaceHolderForLocale" href="$AbsoluteLink" /><% end_loop %><% end_if %>
<% if $LastEdited %><lastmod>$LastEdited.Format(c)</lastmod><% end_if %>
<% if $ChangeFrequency %><changefreq>$ChangeFrequency</changefreq><% end_if %>
<% if $GooglePriority %><priority>$GooglePriority</priority><% end_if %>
</url>
<% end_loop %>
</urlset>
<?php
class LocalisationDataExtension extends DataExtension {
public function getLocalisations() {
$locales = Translatable::get_existing_content_languages();
$currentLocale = Translatable::get_current_locale();
$output = new ArrayList();
foreach($locales as $locale => $name) {
if($page = $this->owner->getRelatedLocalisation($locale, $currentLocale)) {
if($page && $page->exists()) {
$page->PlaceHolderForLocale = $this->updateLocaleForGoogle($locale);
$output->push($page);
}
}
}
return $output;
}
public function updateLocaleForGoogle($locale) {
return strtolower(str_replace("_", "-", $locale));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment