Skip to content

Instantly share code, notes, and snippets.

@HYChou0515
Forked from AWolf81/SpringMVCWebConfig.java
Last active December 27, 2017 03:36
Show Gist options
  • Save HYChou0515/44ca749af367fcc7f0a51538d1f63754 to your computer and use it in GitHub Desktop.
Save HYChou0515/44ca749af367fcc7f0a51538d1f63754 to your computer and use it in GitHub Desktop.
Spring boot configuration file (annotation-based) - configures localization
package mongodbdemo.config;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
/* Configuration for localization of Spring boot app
*
* Place your resource files in src/main/resources/lang/
* and name them
* messages.properties (default fallback)
* messages_de.properties
* messages_en.properties
*
* messages.properties example content:
* welcome=Welcome
*
* In Thymeleaf templates you can use this text as following:
* <h1 th:text="#{welcome}">Welcome</h1>
*
* Switching between locales like this in Thymeleaf template:
* <p>Language : <a href="?language=en">English</a>|<a href="?language=de">German</a></p>
*
*/
@Configuration
@EnableWebMvc
@ComponentScan
public class MvcWebConfig extends WebMvcConfigurerAdapter {
static final Logger logger = LoggerFactory.getLogger(MvcWebConfig.class);
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
@Bean(name = "localeResolver")
public CookieLocaleResolver localeResolver() {
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
Locale defaultLocale = new Locale("en");
localeResolver.setDefaultLocale(defaultLocale);
return localeResolver;
}
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
//looking for absolute file path
messageSource.addBasenames("file:/some/path/to/properties/MessagesBundle");
//looking for application contex (webapp in maven structure)
messageSource.addBasenames("MessagesBundle");
//looking for src/main/resources/
messageSource.addBasenames("classpath:MessagesBundle");
//only work reliably on "file:"
messageSource.setCacheSeconds(10); //reload messages every 10 seconds
return messageSource;
}
// @Bean
// public ResourceBundleMessageSource messageSource() {
// ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
// //looking for src/main/resources/
// messageSource.setBasename("MessagesBundle");
// return messageSource;
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment