Jump to:

3447 Posts in 1032 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » PageByLang function

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

Page: 1
Go to End
Author Topic: 1418 Views
  • chefkoch
    Community Member
    4 Posts

    PageByLang function Link to this post


    i'm having trouble with the PageByLang function explained at

    Well I pasted the code into my Page_Controller and added <% PageByLang(nuestro-despacho,es) %> to my page.ss. But it looks like the parser doesn't run the function. It just puts '<% PageByLang(nuestro-despacho,es) %>' as plain text into my html.

    1. Is 'es' as second parameter correct? In the example it is: <% PageByLang(Kontakt,de) %>, but the locale isn't 'de' it is 'de_DE'

    2. Is there any code it have to put around my function in the template?

    @dev: Is there any example for the usage of this function?

    Regards Chefkoch

  • chefkoch
    Community Member
    4 Posts

    Re: PageByLang function Link to this post

    Well maybe i explain it a bit more precisely:

    <!--<% PageByLang(nuestro-despacho,es_ES) %>--> -> doesn't work
    <% if Locale = es_ES %>
    <h2><a href="./nuestro-despacho/" title="Contacto">Contacto</h2>
    <% else_if Locale = de_DE %>
    <h2><a href="./anwaltsbuero/" title="Kontakt">Kontakt</a></h2>
    <% else_if Locale = en_GB %>
    <h2><a href="./office/" title="Office">Our Office</a></h2>
    <% else_if Locale = ca_ES %>
    <h2><a href="./el-nostre-despatx/" title="Despatx">El nostre despatx</a></h2>
    <% else %>
    <% sprintf(_t('SHOWINPAGE','Show page in %s'),$Locale) %>
    <% end_if %>
    -> hardcoded version depending in <locale> works

    but I have a lot of more links an I don't want to hardcode them all!
    Did I missunderstand the PageByLang function? Does it return a link to the page depending on <locale>?

    When I use it like
    <h2><a href="<% PageByLang(nuestro-despacho,es_ES) %>" title="Contacto">Contacto</h2>
    the function isn't executed at all? Does anybody know why?

    Regards chefkoch
    Thanks for answers =)

  • Willr
    Forum Moderator
    5511 Posts

    Re: PageByLang function Link to this post

    Note the docs have an error - should be <% control PageByLang(..... %>$Whatever<% end_control %>. They have been updated.

  • chefkoch
    Community Member
    4 Posts

    Re: PageByLang function Link to this post

    thank willr. just noticed the changes in the doku and fixed the problem

  • Ironcheese
    Community Member
    33 Posts

    Re: PageByLang function Link to this post

    Hey guys,

    when i try to use the function from the updated tutorial site, it breaks my site with the error message:
    "Fatal error: Call to a member function getTranslation() on a non-object in [...]/mysite/code/Page.php on line 143"

    is there a way to fix this...



    Today i tried it on SilverStripe 2.4 and it worked like a charm. I modified the function a little bit and now it has a nice fallback to english, if the requested page does not exist in the current locale ;)

    public function PageByLang($url, $lang) {
          $SQL_url = Convert::raw2sql($url);
          $SQL_lang = Convert::raw2sql($lang);
          $page = Translatable::get_one_by_lang('SiteTree', $SQL_lang, "URLSegment = '$SQL_url'");
          if ($page->Locale != Translatable::get_current_locale()) {
             // Fallback to English
                $page = $page->getTranslation(Translatable::get_current_locale());
                $page = $page->getTranslation('en_US');
          return $page;

    I need the same mechanism in the normal <% control Menu(x) %> function... but I just can't figure out how :S

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.