Jump to:

3432 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Page name and Translation

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1747 Views
  • Roelfsche
    Avatar
    25 Posts

    Page name and Translation Link to this post

    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>&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:

    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

  • Roelfsche
    Avatar
    25 Posts

    Re: Page name and Translation Link to this post

    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

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Page name and Translation Link to this post

    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

  • Roelfsche
    Avatar
    25 Posts

    Re: Page name and Translation Link to this post

    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

    1747 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.