From 9b7ca7e1dda1664d201565a46c675d1ad287946e Mon Sep 17 00:00:00 2001 From: Pingkan Paula Date: Mon, 5 Mar 2018 21:45:09 +0700 Subject: [PATCH] Implement localization for error messages --- client/src/configureRoutes.tsx | 1 + pom.xml | 5 +++ .../samples/petclinic/config/AppConfig.java | 36 +++++++++++++++++++ .../petclinic/web/LocaleController.java | 15 ++++++++ 4 files changed, 57 insertions(+) mode change 100644 => 100755 client/src/configureRoutes.tsx mode change 100644 => 100755 pom.xml create mode 100644 src/main/java/org/springframework/samples/petclinic/config/AppConfig.java create mode 100644 src/main/java/org/springframework/samples/petclinic/web/LocaleController.java diff --git a/client/src/configureRoutes.tsx b/client/src/configureRoutes.tsx old mode 100644 new mode 100755 index 044dcad2..440a2c0d --- a/client/src/configureRoutes.tsx +++ b/client/src/configureRoutes.tsx @@ -22,6 +22,7 @@ import NotFoundPage from './components/NotFoundPage'; export default () => ( + diff --git a/pom.xml b/pom.xml old mode 100644 new mode 100755 index f2e07de0..70aa7fae --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,10 @@ org.springframework.boot spring-boot-starter-data-jpa + + org.springframework.boot + spring-boot-starter-thymeleaf + org.springframework.boot spring-boot-starter-test @@ -52,6 +56,7 @@ org.springframework.boot spring-boot-starter-web + org.springframework.boot spring-boot-devtools diff --git a/src/main/java/org/springframework/samples/petclinic/config/AppConfig.java b/src/main/java/org/springframework/samples/petclinic/config/AppConfig.java new file mode 100644 index 00000000..e7573816 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/config/AppConfig.java @@ -0,0 +1,36 @@ +package org.springframework.samples.petclinic.config; + +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +import java.util.Locale; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; +import org.springframework.web.servlet.i18n.SessionLocaleResolver; + +@Configuration +public class AppConfig extends WebMvcConfigurerAdapter { + + @Bean + public LocaleResolver localeResolver() { + SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver(); + sessionLocaleResolver.setDefaultLocale(Locale.US); + return sessionLocaleResolver; + } + + @Bean + public LocaleChangeInterceptor localeChangeInterceptor() { + LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); + lci.setParamName("lang"); + return lci; + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(localeChangeInterceptor()); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/web/LocaleController.java b/src/main/java/org/springframework/samples/petclinic/web/LocaleController.java new file mode 100644 index 00000000..c7cfa416 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/web/LocaleController.java @@ -0,0 +1,15 @@ +package org.springframework.samples.petclinic.web; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +public class LocaleController { + + @RequestMapping(value="/locale", method = RequestMethod.GET) + public String getLocalePage() { + return "welcome"; + } + +}