Hi,
I'm working on a multilingual site with SS. I have a problem creating a language chooser.
Therefore i looked inside the code from the widget (http://silverstripe.org/Language-Chooser-Widget-2/)
I changed some method invokation and so my Controller-code looks like:
function LangChooser()
{
if (!isset(Controller::curr()->ID))
{
return; // So it doesn't break on Controller-less pages
}
$langs = Translatable::get_existing_content_languages();
$data = new DataObjectSet();
foreach (array_keys($langs) as $code)
{
if (i18n::get_lang_from_locale($code) == Translatable::current_lang())
{
$css_klasse = 'sel_lang';
}
else
{
$css_klasse = '';
}
$page = Translatable::get_one_by_locale("SiteTree", $code, "`SiteTree`.ID = " . Controller::curr()->ID);
if (!$page)
{
$data->push(new ArrayData(array('css_klasse' => $css_klasse, 'sprache' => i18n::get_lang_from_locale($code), 'link' => Director::protocolAndHost() . Director::baseURL() . '?locale=' . $code)));
} else
{
$data->push(new ArrayData(array('css_klasse' => $css_klasse, 'sprache' => i18n::get_lang_from_locale($code), 'link' => Director::protocolAndHost() . Director::baseURL() . $page->URLSegment . '?locale=' . $code)));
}
}
return $data;
}
In my template I only need to do the following:
<% control LangChooser %>
<% if Last %>
<a class="$css_klasse" href="$link"> $sprache </a>
<% else %>
<a class="$css_klasse" href="$link"> $sprache </a> /
<% end_if %>
<% end_control %>
I used 'de_DE' as default-language.
Thats why my home page has 'home' as site name and the english version 'home-2'. If I now switch to english, the link back to german looks like:
http://mysite.localhost/home-2?locale=de_DE
This doesn't work, because 'home-2' stands for the english version and there is no german version for that.
Do I have to select the appropriate translation of the page from db to retrieving the right URL or is there a method that does the job for me?
Thank you,
roelfsche