title: Retrieve locale based on the Accept-Language in Spring Boot
body: <p>I have a Spring Boot (<code>2.1.3.RELEASE</code>) application that uses Jersey to define the (RESTful) endpoints. I'm trying to read and propagate some messages based on the locale being sent by the user-agents.</p>
  
  <p>I've configured these beans:</p>
  
  <pre><code>@Bean
  public LocaleResolver localeResolver() {
    final AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
    resolver.setSupportedLocales(Arrays.asList(Locale.GERMANY, Locale.US));
    resolver.setDefaultLocale(Locale.ENGLISH);
    return resolver;
  }
  
  @Bean
  public MessageSource messageSource() { // Not sure if this is needed
    final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
    messageSource.setBasenames("classpath:/messages");
    messageSource.setUseCodeAsDefaultMessage(true);
    messageSource.setCacheSeconds(5);
    return messageSource;
  }
  </code></pre>
  
  <p>...and also the bundles (inside <code>../src/main/resources/</code>) like: <code>messages.properties</code> (fallback), <code>messages_en_US.properties</code>, <code>messages_de_DE.properties</code>, etc.</p>
  
  <p>Now, the challenge is that I'm not sure how to "read" the locale sent by the user-agents in order to read the messages from the bundles appropriately. I'm injecting a <code>MessageSource ms</code>, and programmatically reading messages like:</p>
  
  <pre><code>final Locale locale = ???
  ms.getMessage("message.duplicate-token", null, locale);
  </code></pre>
  
  <p>Any clues?</p>
  
  <blockquote>
    <p>I've tried <code>LocaleContextHolder.getLocale()</code> but it's always <code>en_US</code>. If I hardcode the corresponding locale for the <code>getMessage</code> call, I'm able to retrieve the correct message(s). So I know the setup/configuration works for the most part.</p>
  </blockquote>
  
  <hr>
  
  <p>Clients are sending the locale using the <code>Accept-Language</code> header — and values like: <code>de-DE</code>, <code>en-US</code>, etc.</p>