title: Spring boot with i18n (internationalization) and thymeleaf
body: <p>I am trying to get i18n to work with my spring boot application, which uses thymeleaf as template engine.</p>
  
  <p>I followed some tutorials, which showed me how to define message source and locale resolver, so I made this configuration class:</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  public class AppConfig extends WebMvcConfigurerAdapter {
  
      @Bean
      public MessageSource messageSource() {
          ReloadableResourceBundleMessageSource msgSrc = new ReloadableResourceBundleMessageSource();
          msgSrc.setBasename("i18n/messages");
          msgSrc.setDefaultEncoding("UTF-8");
          return msgSrc;
      }
  
      @Bean
      public LocaleResolver localeResolver() {
          CookieLocaleResolver resolver = new CookieLocaleResolver();
          resolver.setDefaultLocale(new Locale("en"));
          resolver.setCookieName("myI18N_cookie");
          return resolver;
      }
  
      @Override
      public void addInterceptors(InterceptorRegistry reg) {
          LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
          interceptor.setParamName("locale");
          reg.addInterceptor(interceptor);
      }
  
  }
  </code></pre>
  
  <p>Then, in my resource folder (src/main/resources) i made folder <strong>i18n</strong> and inside i put <strong>messages.properties</strong> and <strong>messages_sl.properties</strong></p>
  
  <p>inside there is defined <strong>first.greeting = Hello World!</strong></p>
  
  <p>And this is my thymeleaf template:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" th:with="lang=${#locale.language}" th:lang="${lang}"&gt;
      &lt;head&gt;
          &lt;meta charset="UTF-8"/&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;a href="/?locale=en"&gt;English&lt;/a&gt; | &lt;a href="/?locale=sl"&gt;Slovenian&lt;/a&gt;
          &lt;h3 th:text="#{first.greeting}"&gt;&lt;/h3&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My Controller is nothing special, it just forwards this view when I access it and in my properties file i have defined:</p>
  
  <pre><code>spring.thymeleaf.mode=LEGACYHTML5
  spring.thymeleaf.cache=false
  </code></pre>
  
  <p>However, when I load page, instead of <strong>Hello World!</strong> I get <strong>??first.greeting_en??</strong> or <strong>??first.greeting_sl??</strong>, depending on set locale.</p>
  
  <p>Everywhere I looked I saw same configuration, so I am really lost as what am I missing.</p>
  
  <p>And here is my project structure:</p>
  
  <pre><code> src
     └───  main
         ├───  java
         │   └───  com
         │       └───  mjamsek
         │           └───  Simple_i18n
         │               ├───  conf
         │               └───  controller
         └───  resources
             ├───  i18n
             │   ├─── messages.properties
             │   └─── messages_sl.properties
             ├───  static
             └───  templates
  </code></pre>