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.

Form Questions

LoginForm not maintaining the right locale


5 Posts   617 Views


23 November 2011 at 9:33pm Community Member, 7 Posts

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.


24 November 2011 at 12:28am Community Member, 288 Posts

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.


24 November 2011 at 1:11am Community Member, 7 Posts

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


24 November 2011 at 2:31am Community Member, 288 Posts

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

public function 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.


16 January 2012 at 6:45pm Community Member, 105 Posts

Thanks Smurkas that helped a lot...