diff --git a/whatsmars-spring-boot/src/main/java/com/itlong/whatsmars/spring/boot/UserController.java b/whatsmars-spring-boot/src/main/java/com/itlong/whatsmars/spring/boot/UserController.java index 873716243db9d5788eb2f6f79b49e44b43dc02a6..3f485bbecd7508ea9a3b89c53ddb8ba0a63b68af 100644 --- a/whatsmars-spring-boot/src/main/java/com/itlong/whatsmars/spring/boot/UserController.java +++ b/whatsmars-spring-boot/src/main/java/com/itlong/whatsmars/spring/boot/UserController.java @@ -1,10 +1,16 @@ package com.itlong.whatsmars.spring.boot; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.MessageSource; +import org.springframework.context.annotation.Bean; +import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.i18n.FixedLocaleResolver; +import java.util.Locale; import java.util.Map; /** @@ -16,9 +22,26 @@ public class UserController { @Autowired private UserConfig userConfig; + @Autowired + private MessageSource messageSource; + + /** + * 设置区域解析器 (default is AcceptHeaderLocaleResolver) + */ + @Bean + public LocaleResolver localeResolver() { + FixedLocaleResolver slr = new FixedLocaleResolver (); + slr.setDefaultLocale(Locale.US); + return slr; + + } + @RequestMapping("/") public String home(Map map) { map.put("hello", "Hi, boy!"); + Locale locale = LocaleContextHolder.getLocale(); + String country = messageSource.getMessage("country", null, locale); + map.put("country", country); return "index"; } diff --git a/whatsmars-spring-boot/src/main/resources/application-test.properties b/whatsmars-spring-boot/src/main/resources/application-test.properties index 650146b55a1a1e9adad606a6065bd86ca3b3065e..2152339eaf173bfa876e7a197324055f91054072 100644 --- a/whatsmars-spring-boot/src/main/resources/application-test.properties +++ b/whatsmars-spring-boot/src/main/resources/application-test.properties @@ -60,3 +60,6 @@ user.welcome=Hello, World! user.noFilterUrl=/,/login spring.thymeleaf.cache=false + +#i18n +spring.messages.basename=i18n/messages diff --git a/whatsmars-spring-boot/src/main/resources/i18n/messages.properties b/whatsmars-spring-boot/src/main/resources/i18n/messages.properties new file mode 100644 index 0000000000000000000000000000000000000000..645537cf939a723d0491c13c96c2bae9f4951ab8 --- /dev/null +++ b/whatsmars-spring-boot/src/main/resources/i18n/messages.properties @@ -0,0 +1 @@ +country=中国 \ No newline at end of file diff --git a/whatsmars-spring-boot/src/main/resources/i18n/messages_en_US.properties b/whatsmars-spring-boot/src/main/resources/i18n/messages_en_US.properties new file mode 100644 index 0000000000000000000000000000000000000000..9bc78b14c3aeca2b6803c94c1614b1794445df28 --- /dev/null +++ b/whatsmars-spring-boot/src/main/resources/i18n/messages_en_US.properties @@ -0,0 +1 @@ +country=China \ No newline at end of file diff --git a/whatsmars-spring-boot/src/main/resources/i18n/messages_zh_CN.properties b/whatsmars-spring-boot/src/main/resources/i18n/messages_zh_CN.properties new file mode 100644 index 0000000000000000000000000000000000000000..2a9b85d2b7f59d3e15df14b5bb7758e0328f543e --- /dev/null +++ b/whatsmars-spring-boot/src/main/resources/i18n/messages_zh_CN.properties @@ -0,0 +1 @@ +country=\u4e2d\u56fd \ No newline at end of file diff --git a/whatsmars-spring-boot/src/main/resources/templates/index.html b/whatsmars-spring-boot/src/main/resources/templates/index.html index e2408fb2ac898a3997b6f22b5d6affe54d553a7e..17163e051109b45879335f2ec65362dae27c2a9d 100644 --- a/whatsmars-spring-boot/src/main/resources/templates/index.html +++ b/whatsmars-spring-boot/src/main/resources/templates/index.html @@ -7,5 +7,7 @@

thymeleaf

+

+

\ No newline at end of file