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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

LoginForm not maintaining the right locale


Go to End


5 Posts   1741 Views

Avatar
Joures

Community Member, 7 Posts

23 November 2011 at 9:33pm

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.

Avatar
MarcusDalgren

Community Member, 288 Posts

24 November 2011 at 12:28am

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.

Avatar
Joures

Community Member, 7 Posts

24 November 2011 at 1:11am

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

Avatar
MarcusDalgren

Community Member, 288 Posts

24 November 2011 at 2:31am

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.

Avatar
CodeGuerrilla

Community Member, 105 Posts

16 January 2012 at 6:45pm

Thanks Smurkas that helped a lot...