This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RequestParamLocaleContextResolver implements LocaleContextResolver{ | |
@Override | |
public LocaleContext resolveLocaleContext(ServerWebExchange exchange) { | |
Locale targetLocale = Locale.getDefault(); | |
List<String> referLang = exchange.getRequest().getQueryParams().get("lang"); | |
if (referLang != null && !referLang.isEmpty()) { | |
String lang = referLang.get(0); | |
targetLocale = Locale.forLanguageTag(lang); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class LocaleSupportConfig extends DelegatingWebFluxConfiguration { | |
@Override | |
protected LocaleContextResolver createLocaleContextResolver() { | |
return new RequestParamLocaleContextResolver(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>I18N Spring Webflux</title> | |
</head> | |
<body> | |
<h2 data-th-text="#{label.welcome}"></h2> | |
<p data-th-text="#{label.content}"></p> | |
<p data-th-text="#{label.changeLang}"></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class WelcomeHandler { | |
public Mono<ServerResponse> hello(ServerRequest request) { | |
return ServerResponse | |
.ok() | |
.contentType(MediaType.TEXT_HTML) | |
.render("index"); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) | |
.andRoute(RequestPredicates.GET("/welcome") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.1.1.RELEASE</version> | |
<relativePath/> | |
</parent> | |
<dependencies> | |
<dependency> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table IF NOT EXISTS languages ( | |
id integer auto_increment, | |
locale varchar(2), | |
messagekey varchar(255), | |
messagecontent varchar(255), | |
primary key (id) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSERT INTO languages (locale, messagekey,messagecontent) VALUES | |
('en', 'home.welcome','Welcome'), | |
('en', 'home.info','This page is displayed in English.'), | |
('en', 'home.changelanguage','Supported languages : '), | |
('en', 'home.lang.en','English'), | |
('en', 'home.lang.de','German'), | |
('en', 'home.lang.zh','Chinese'), | |
('de', 'home.welcome','Welcome'), | |
('de', 'home.info','Diese Seite wird in deutscher Sprache angezeigt.'), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE | |
spring.datasource.username=sa | |
spring.datasource.password= | |
spring.datasource.driverClassName=org.h2.Driver | |
spring.jpa.hibernate.ddl-auto=update | |
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect | |
spring.h2.console.path=/myconsole | |
spring.h2.console.enabled=true | |
spring.datasource.initialize=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class WebConfig implements WebMvcConfigurer { | |
@Bean | |
public LocaleResolver localeResolver() { | |
return new CookieLocaleResolver(); | |
} | |
@Override | |
public void addInterceptors(InterceptorRegistry registry) { |