Based on the modification of the language switcher of Juanito in this post: http://www.silverstripe.org/releases-and-announcements/show/260948?start=0 I wrote my custom language switcher.
Here is how the language switcher works:
1. it gets the default language of the site
2. it gets all translated versions of the current page
3. it generates a DataObjectSet with the locale code, language name, urlSegment and link to each translation of the page
Just place the following function in the code of your PageClass:
/**
* Returns a DataObjectSet with all available translations of the current page
* @return DataObjectSet
*/
function getTranslationsFunc() {
if($this->URLSegment == "Security") return "";
$langs = Translatable::get_existing_content_languages();
$data = new DataObjectSet();
// generate entry with only the defaul language if no translations of this page are available
$availTransl = Translatable::getTranslations();
if(count($availTransl) == 0) {
$code = Translatable::default_locale();
$data->push(new ArrayData(array(
'name' => i18n::get_language_name(i18n::get_lang_from_locale($code), true),
'link' => Director::baseURL() . $this->URLSegment,
'urlSegment' => $this->URLSegment,
'code' => $code,
'current' => 'current"'
)));
}
else {
foreach(array_keys($langs) as $code) {
// get country code (e.g. "de")
$lang = i18n::get_lang_from_locale($code);
// current language?
$current = ($code == Translatable::get_current_locale()) ? 'current': '';
$page = $this->getTranslation($code);
$data->push(new ArrayData(array(
'name' => i18n::get_language_name($lang, true),
'link' => Director::baseURL() . $page->URLSegment,
'urlSegment' => $page->URLSegment,
'code' => $code,
'current' => $current
)));
}
}
return $data;
}
Then place this code in your Page Controller (the controller just calls the class function):
/**
* Returns a DataObjectSet with all available translations of the current page
* @return DataObjectSet
*/
function getTranslations() {
// call method of Page Object
return $this->getTranslationsFunc();
}
Then you can call this Controller Function from your template like this:
<% if getTranslations %>
<ul>
<% control getTranslations %>
<li class="$selected"><a href="$link">$name</li>
<% end_control %>
</ul>
<% end_if %>