Max Vasileuski
Max Vasileuski

Reputation: 2651

Can't receive user locale in the service

I have two property files for i18n and want to receive localized message in service method. I've injected messageSource and now all I want is to obtain user's locale.

  1. First, I tried to do this over LocaleContextHolder.getLocale() but it returns my system locale regardless of the request locale.
  2. Then I've injected HttpServletRequest into service and tried this: RequestContextUtils.getLocaleResolver(request).resolveLocale(request), but didn't even receive LocaleResolver (it was null).

I can't send locale someway from controller to this service, because it's just implementation of Spring Security's UserDetailsService

And I use CookieLocaleResolver in my project, if it matters.

Upvotes: 1

Views: 235

Answers (1)

soulcheck
soulcheck

Reputation: 36767

In general you want to leave presentation problems to the presentation layer.

In your case, since you want to display the message in the jsp, use <spring:message /> (or plain <fmt:message />) tag in that jsp and let spring worry about the l10n.

If you can't do that, another option is throwing an exception from your service layer and handling it in controller/interceptor where you have access to ServletRequest and, in consequence, to the user's locale.

Upvotes: 1

Related Questions