Skip to content

Instantly share code, notes, and snippets.

@linhvovn
linhvovn / spring-webflux-i18n spring-boot-parent
Last active December 24, 2018 16:51
Introduction to I18N in Spring WebFlux
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
@Configuration
@EnableWebFlux
public class WebConfig implements ApplicationContextAware, WebFluxConfigurer {
ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext context) {
this.context = context;
}
//...
@Bean
public ITemplateResolver thymeleafTemplateResolver() {
final SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(this.context);
resolver.setPrefix("classpath:views/");
resolver.setSuffix(".html");
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCacheable(false);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>I18N Spring Webflux</title>
</head>
<body>
<p>Hi there, you have successfully setup application with Spring Boot, Spring Webflux and Thymleaf</p>
</body>
</html>
@Component
public class WelcomeHandler {
public Mono<ServerResponse> hello(ServerRequest request) {
return ServerResponse
.ok()
.contentType(MediaType.TEXT_HTML)
.render("index");
}
//...
@Configuration
public class WelcomeRouter {
@Bean
public RouterFunction<ServerResponse> route(WelcomeHandler welcomeHandler) {
return RouterFunctions
.route(RequestPredicates.GET("/index")
.and(RequestPredicates.accept(MediaType.TEXT_HTML)), welcomeHandler::hello);
@SpringBootApplication
public class I18NWebfluxApplication {
public static void main(String[] args) {
SpringApplication.run(I18NWebfluxApplication.class, args);
}
}
label.welcome = Welcome
label.content = This is sample project for Internalization and Localization in Webflux
label.changeLang = Supported languages
label.lang.en = English
label.lang.fr = French
label.lang.cn = Chinese
label.lang.de = German
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("languages/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}