Changing locale of user in home page of Grails application -
i've been trying change user locale after logging in (i use spring security).
i have tried changing locale in success event handler of spring security (oninteractiveauthenticationsuccessevent) , creating own localeresolver , registering on resources.groovy.
it seems work fine pages except home page.
this mylocaleresolver
class mylocaleresolver implements localeresolver { def springsecurityservice private locale defaultlocale = null; @override locale resolvelocale(httpservletrequest httpservletrequest) { user user = user.findbyemail(springsecurityservice.authentication.principal.username) if (user && !defaultlocale) { if (user.lang) { this.defaultlocale = new locale(user.lang) } else { this.defaultlocale = locale.english } } println "defaultlocale = $defaultlocale" return this.defaultlocale } @override void setlocale(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, locale locale) { this.defaultlocale = locale; } }
and resources.groovy part
localeresolver(mylocaleresolver){ springsecurityservice = ref("springsecurityservice") }
update (07/04/2015): have tried extend cookielocaleresolver, , because sets cookie value, works fine in cases except in first home page load default language set instead of 1 set in resolvelocale.
class mylocaleresolver extends cookielocaleresolver{ def springsecurityservice @override locale resolvelocale(httpservletrequest httpservletrequest) { super.resolvelocale(httpservletrequest) user user = user.findbyemail(springsecurityservice.authentication.principal.username) locale locale if (user) { if (user.lang) { locale = new locale(user.lang) } else { locale = locale.english } httpservletrequest.setattribute(locale_request_attribute_name, locale != null?locale:this.determinedefaultlocale(request)); } return (locale)httpservletrequest.getattribute(locale_request_attribute_name); }
}
i have tried differente approach, although think it's better try override localeresolver. have tried change locale when users login, changing defaultlocale in method loaduserbyusername of mongouserdetailsservice. happens same previous approach, works except on first home page load after loggin in.
def localeresolver @override public userdetails loaduserbyusername(string username) throws usernamenotfoundexception { user.withtransaction { status -> user user = user.findbyemail(username) if (!user) throw new usernamenotfoundexception( 'user not found', username) try { localeresolver.setdefaultlocale(user.lang ? new locale(user.lang) : locale.english) } catch (exception e){ e.printstacktrace() } def authorities = user.authorities.collect { new simplegrantedauthority(it.authority) } return new myuserdetails(user.username, user.password, user.enabled, !user.accountexpired, !user.passwordexpired, !user.accountlocked, authorities ?: mongouserdetailsservice.no_roles, user.id, user.client, user.email, user.datecreated) } }
and resources.groovy
userdetailsservice(net.ds.batto.mongouserdetailsservice){ localeresolver = ref("localeresolver") }
also, thought grails used cookielocaleresolver default, have seen localeresolver reference of of sessionlocaleresolver. right behaviour or may there misconfigured?
to me defaultlocale global configuration whole application (or @ least not particular user session)
custom controllers can override user's locale , time zone calling #setlocale(context) on resolver
so maybe can try replace
localeresolver.setdefaultlocale(user.lang ? new locale(user.lang) : locale.english)
by
class mylocalechangeinterceptor extends localechangeinterceptor { ... @override boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) { localeresolver.setlocale(user.lang ? new locale(user.lang):locale.english) } }
directly in localechangeinterceptor, guess it's late when server reaches 'localeresolver' request has been processed, following request affected locale change.
Comments
Post a Comment