3341 Posts in 982 Topics by 703 members
|Go to End|
10 January 2010 at 10:07am Last edited: 10 January 2010 11:28am
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:
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';
$css_klasse = '';
$page = Translatable::get_one_by_locale("SiteTree", $code, "`SiteTree`.ID = " . Controller::curr()->ID);
$data->push(new ArrayData(array('css_klasse' => $css_klasse, 'sprache' => i18n::get_lang_from_locale($code), 'link' => Director::protocolAndHost() . Director::baseURL() . '?locale=' . $code)));
$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)));
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:
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?
10 January 2010 at 9:23pm Last edited: 10 January 2010 9:24pm
I found the solution. For every language I retrieve the appropriate page from db and take the URLSegment.
Anyway, it looks like doing the job twice: first retrieving all translations and second retrieve the page for every translation for getting the right URLSegment.
Do I think right?
25 January 2010 at 12:30pm
If you want to limit the language chooser to translations which actually exist for the current page, here's a recipe:
25 January 2010 at 10:56pm
I've red that receipe but now I see, i didn't understood. Obviously the
makes the things I need.
|Go to Top|