ENHANCEMENT Added Translatable->MetaTags() to automatically insert <link rel="alternate" hreflang="...> tags into the page template

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@75349 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-04-28 00:08:14 +00:00
parent 99c1fe9f93
commit a8028df67e

View File

@ -63,8 +63,7 @@
* Caution: The "URLSegment" property is enforced to be unique across
* languages by auto-appending the language code at the end.
* You'll need to ensure that the appropriate "reading language" is set
* before showing links to other pages on a website: Either
* through setting $_COOKIE['locale'], $_SESSION['locale'] or $_GET['locale'].
* before showing links to other pages on a website through $_GET['locale'].
* Pages in different languages can have different publication states
* through the {@link Versioned} extension.
*
@ -171,7 +170,7 @@ class Translatable extends DataObjectDecorator {
/**
* Choose the language the site is currently on.
* If $_GET['locale'] or $_COOKIE['locale'] is set, then it will use that language, and store it in the session.
* If $_GET['locale'] is set, then it will use that language, and store it in the session.
* Otherwise it checks the session for a possible stored language, either from namespace to the site_mode
* ('site' or 'cms'), or for a 'global' language setting.
* The final option is the member preference.
@ -962,6 +961,27 @@ class Translatable extends DataObjectDecorator {
return $children;
}
/**
* Returns <link rel="alternate"> markup for insertion into
* a HTML4/XHTML compliant <head> section, listing all available translations
* of a page.
*
* @see http://www.w3.org/TR/html4/struct/links.html#edef-LINK
*
* @return string HTML
*/
function MetaTags(&$tags) {
$template = '<link rel="alternate" type="text/html" title="%s" hreflang="%s" href="%s">' . "\n";
$translations = $this->owner->getTranslations();
if($translations) foreach($translations as $translation) {
$tags .= sprintf($template,
$translation->Title,
i18n::convert_rfc1766($translation->Locale),
$translation->Link()
);
}
}
/**
* If called with default language, doesn't affect the results.
* Otherwise (called in translation mode) the method tries to find translations