提交 453c3929 编写于 作者: S shenhongxi

i18n

上级 4bf9bd11
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<String,Object> map) {
map.put("hello", "Hi, boy!");
Locale locale = LocaleContextHolder.getLocale();
String country = messageSource.getMessage("country", null, locale);
map.put("country", country);
return "index";
}
......
......@@ -60,3 +60,6 @@ user.welcome=Hello, World!
user.noFilterUrl=/,/login
spring.thymeleaf.cache=false
#i18n
spring.messages.basename=i18n/messages
......@@ -7,5 +7,7 @@
<body>
<h1 th:inline="text">thymeleaf</h1>
<p th:text="${hello}"></p>
<p th:text="${country}"></p>
<p th:text="#{country}"></p>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册