Jump to:

1971 Posts in 1275 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » [solved] Director::redirect( The actual page but with the current member locale )

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

Page: 1
Go to End
Author Topic: 1284 Views
  • VRoxane
    Avatar
    Community Member
    41 Posts

    [solved] Director::redirect( The actual page but with the current member locale ) Link to this post

    Hello !

    On log in, I want the current page viewed to be "translated" (the user is redirected to the page in his own language/locale).
    I already put this in the Page.php :

       $member = Member::currentUser();
          if($member && $member->Locale) {
             //Translatable::set_locale($member->Locale);
             Translatable::set_current_locale($member->Locale);


    and when I log in, the menus get translated, but not the content.

    So, I got this code on the amazing SSbits site :
    http://www.ssbits.com/snippets/2010/customize-the-redirect-after-a-successful-member-login/

    And I'm trying to modify it so when I log in, the site refreshes to the same page (ex: /contact), but with my locale defined as the current locale (/contact?locale=en_GB) so I would be redirected to /contact-en-gb.

    The last line is what I invented wrote myself :

    /// In LoginLocale.php :
    class LoginLocale extends MemberLoginForm {
    public function dologin($data) {
    parent::dologin($data);
    if( Director::redirected_to() and Member::currentUserID() ) {
    $this->controller->response->removeHeader('Location');
             $member = Member::currentUser();
             $member_locale = $member->Locale;
    Director::redirect(Director::baseURL() . Director::get_current_page()->UrlSegment . "?locale=" . $member_locale);
    }
    }
    }

    /// And in _config :

       Object::useCustomClass('MemberLoginForm', 'LoginLocale');

    I manage to set the locale, but the Current Page Url doesn't show anything -> I get to the homepage.

    Can somebody help me with the last line ?

  • VRoxane
    Avatar
    Community Member
    41 Posts

    Re: [solved] Director::redirect( The actual page but with the current member locale ) Link to this post

    Here's the solution :

    For the last line, you can use this (what I managed to write without really knowing what I was doin') :

    $params = Director::urlparams();
    Director::redirect(Director::baseURL() . $params['URLSegment'] . "?locale=" . $member_locale);

    or this (it's shorter and very simple... that's the bit of code I was searching. Thanks to Zauberfisch on #silverstripe !) :

    Director::redirect(Director::get_current_page()->Link() . "?locale=" . $member_locale);

    1284 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.