Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Page name and Translation


Reply


4 Posts   1783 Views

Avatar
Roelfsche

25 Posts

10 January 2010 at 10:07am

Edited: 10/01/2010 11:28am

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 ([url]http://silverstripe.org/Language-Chooser-Widget-2/[/url])
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>&nbsp;/&nbsp;
<% 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

Avatar
Roelfsche

25 Posts

10 January 2010 at 9:23pm

Edited: 10/01/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?
Thanks,
roelfsche

Avatar
Ingo

Forum Moderator, 801 Posts

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:
http://doc.silverstripe.org/doku.php?id=multilingualcontent#templates

Avatar
Roelfsche

25 Posts

25 January 2010 at 10:56pm

Hello Ingo,
I've red that receipe but now I see, i didn't understood. Obviously the

hreflang="$Locale.RFC1766"


makes the things I need.
Thank you,
Roelfsche