title: Spring: How to get the "ACTUAL" / "CURRENT" locale like it works for messages?
body: <p>I am a bit puzzled, as I have a working system but I cannot get the actual/current locale.</p>
  
  <p>The working system is about this:</p>
  
  <p>For example from: <a href="http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/" rel="nofollow noreferrer">http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/</a></p>
  
  <p>Some messages.properties file,</p>
  
  <p>A servlet-context.xml file with:</p>
  
  <pre><code>  &lt;!-- Additional i18n --&gt;
    &lt;bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.SessionLocaleResolver"&gt;
      &lt;property name="defaultLocale" value="en" /&gt;
    &lt;/bean&gt;
  
    &lt;bean id="translationService" class="inbiz.webapp.service.TranslationService"&gt;
    &lt;/bean&gt;
    &lt;alias name="translationService" alias="t" /&gt;
  
    &lt;mvc:interceptors&gt;
      &lt;bean id="localeChangeInterceptor" 
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt;
        &lt;property name="paramName" value="lang" /&gt;
      &lt;/bean&gt;
    &lt;/mvc:interceptors&gt;
    &lt;!-- END: Additional i18n --&gt;
  </code></pre>
  
  <p>For the TranslationService (even if not directly related to the question check also: <a href="https://stackoverflow.com/questions/29657397/java-spring-mvc-service-method-nullpointerexception-newbie-and-translation">java spring mvc @service method: nullpointerexception | newbie and translation</a>)</p>
  
  <p>This apparently makes already everything working within the like of the first link. So I can do:</p>
  
  <pre><code>&lt;a href="" th:text="#{help}"&gt;Help&lt;/a&gt;
  </code></pre>
  
  <p>This is thymeleaf (<a href="http://www.thymeleaf.org/" rel="nofollow noreferrer">http://www.thymeleaf.org/</a>) but basically the same as JSP or JSF .. The messages get loaded with the correct locale.</p>
  
  <p>But for my TranslationService I need to get the locale just like the messages bundle does.</p>
  
  <p>Following: <a href="https://templth.wordpress.com/2010/07/21/configuring-locale-switching-with-spring-mvc-3/" rel="nofollow noreferrer">https://templth.wordpress.com/2010/07/21/configuring-locale-switching-with-spring-mvc-3/</a>, I set up this filter:</p>
  
  <pre><code>package inbiz.webapp.filters;
  
  import java.io.IOException;
  
  import java.util.Map;
  import java.util.Locale;
  
  import javax.servlet.FilterChain;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  
  import org.springframework.web.context.WebApplicationContext;
  import org.springframework.web.context.support.WebApplicationContextUtils;
  import org.springframework.web.servlet.LocaleResolver;
  
  import org.springframework.context.i18n.LocaleContextHolder;
  
  import org.springframework.web.servlet.support.RequestContextUtils;
  
  import org.springframework.web.filter.OncePerRequestFilter;
  
  public class LocaleConfigurerFilter extends OncePerRequestFilter {
  
      private LocaleResolver localeResolver;
  
  
      protected void initFilterBean() throws ServletException {
  
      Locale locale;
  
          WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
          Map resolvers = wac.getBeansOfType(LocaleResolver.class);
          if (resolvers.size()==1) {
              localeResolver = (LocaleResolver) resolvers.values().iterator().next();
          }
  
      locale = LocaleContextHolder.getLocale();
      System.out.println("DEBUG FILTER LOCALE LCH - INIT: "+locale);
  
      }
  
  
      @Override
      protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
      throws IOException, ServletException {
  
      Locale locale;
  
      WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
  
          Map resolvers = wac.getBeansOfType(LocaleResolver.class);
          if (resolvers.size()==1) {
              localeResolver = (LocaleResolver) resolvers.values().iterator().next();
          }
          if (localeResolver!=null) {
              locale = localeResolver.resolveLocale(req);
          System.out.println("DEBUG FILTER LOCALE LRS: "+locale);
              LocaleContextHolder.setLocale(locale);
          }
  
      locale = LocaleContextHolder.getLocale();
      System.out.println("DEBUG FILTER LOCALE LCH: "+locale);
      locale = RequestContextUtils.getLocale((HttpServletRequest) req);
      System.out.println("DEBUG FILTER LOCALE RCU: "+locale);
      LocaleContextHolder.setLocale(locale);
  
      chain.doFilter(req, res);
  
      }
  
  }
  </code></pre>
  
  <p>The filter gets called, but the output is just:</p>
  
  <pre><code>DEBUG FILTER LOCALE LCH - INIT: en_US
  DEBUG FILTER LOCALE LCH: en_US
  DEBUG FILTER LOCALE RCU: en_US
  DEBUG FILTER LOCALE LCH: en_US
  DEBUG FILTER LOCALE RCU: en_US
  DEBUG FILTER LOCALE LCH: en_US
  DEBUG FILTER LOCALE RCU: en_US
  </code></pre>
  
  <p>Even if any page is called with something like: ?lang=it_IT</p>
  
  <p>(which indeed works for the messages.properties)</p>
  
  <p>I am sorry it might be too many information... but as I say I am a bit lost in understanding who keeps the actual locale. Also as the next step will be to set the locale from the user profile, but I need a controlled default if the user is not logged in.</p>
  
  <p>So the final question: How do I get the locale?</p>
  
  <p>I think, I miss some fundamental points ... and the solution is just one line. But at present I finished my "web" resources.. ;) Maybe a wrong search..</p>
  
  <p>On the other side I give a bit of information, so that it might help
  other people trying to implement a "gnu gettext" version.</p>