Closed JuKu closed 5 years ago
Hi
I have already solved the issue. This code is working for me:
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
registry.addInterceptor(localeChangeInterceptor);
//i18n
registry.addInterceptor(i18nMessageInterceptor());
}
@Bean
public LocaleResolver localeResolver() {
//see also: https://www.baeldung.com/spring-security-login-error-handling-localization
CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
//By default, the locale resolver will obtain the locale code from the HTTP header. To force a default locale, we need to set it on the localeResolver():
//cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);
return cookieLocaleResolver;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource =
new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setUseCodeAsDefaultMessage(true);
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(0);
return messageSource;
}
@Bean
public org.springframework.web.servlet.view.mustache.jmustache.LocalizationMessageInterceptor i18nMessageInterceptor () {
org.springframework.web.servlet.view.mustache.jmustache.LocalizationMessageInterceptor lmi = new org.springframework.web.servlet.view.mustache.jmustache.LocalizationMessageInterceptor();
lmi.setLocaleResolver(localeResolver());
lmi.setMessageSource(messageSource());
lmi.setMessageKey("i18n");
return lmi;
}
Hi,
i'm using JMustache with Spring Boot and want to support localization. The example in the ReadMe did't work.
My template contains a i18n string:
And i tried to configure spring & JMustache to use the correct dataSource:
But in template there is only a " " (empty space). If i print the string in my controller (with @AutoWired MessageSource) it work's fine, i get the correct string:
So what's the problem? How can i use localization with Spring Boot and JMustache? Thanks a lot!