Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » LoginForm not maintaining the right locale

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

Page: 1
Go to End
Author Topic: 574 Views
  • Joures
    Avatar
    Community Member
    7 Posts

    LoginForm not maintaining the right locale Link to this post

    If i set the locale of my website from swedish to english, it works allright except for the loginform.
    It somehow forces the locale to go back to it's default setting (wich is swedish). How can i fix this?
    I'm not using any custom login form or something, just the form supplied by sapphire.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: LoginForm not maintaining the right locale Link to this post

    The problem is that if you're setting the locale of the site in the init method of your Page_Controller as you usually do when you have a multilingual site then it doesn't affect Security since Security extends Controller, not Page_Controller. There's extension points in Controller, one called onBeforeInit and one called onAfterInit.

    I think you can hook into onBeforeInit and set the site locale then and it will affect the login form as well. If onBeforeInit proves to be too early then try onAfterInit, I know one of them works.

  • Joures
    Avatar
    Community Member
    7 Posts

    Re: LoginForm not maintaining the right locale Link to this post

    I kinda got what you mean. But can you show me an example, i'm new to Silverstripe

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: LoginForm not maintaining the right locale Link to this post

    So I'm kind of assuming that you have something like this in your Page_Controller:

    public function init() {
       i18n::set_locale(Translatable::get_current_locale());
       parent::init();
    }

    And this generally works since the rest of your page classes extends the Page/Page_Controller classes.
    However like I said, Security doesn't extend Page_Controller, it extends Controller.
    In order to affect it we need to hook into Controller.

    First we make a new class that's going to hook into Controller like this for example:

    class LoginFormLanguageSetter extends Extension {
       function onBeforeInit() {
          i18n:: set_locale(Translatable::get_current_locale());
       }
    }

    Now in order to make this run you need to put the following in your _config.php:

    Object::add_extension('Controller', 'LoginFormLanguageSetter');

    The function name in LoginLanguageSetter matches the name of the function in the $this->extend() call in Controller on line 132.

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: LoginForm not maintaining the right locale Link to this post

    Thanks Smurkas that helped a lot...

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