3国际化 · SpringCloud微服务实战 · 看云
导航
本节代码地址
GitHub: https://github.com/xuyisu/fw-sping-cloud/tree/master/fw-cloud-springboot/fw-springboot-international
1.1 新建项目
为了方便后续阅读我们新建模块fw-springboot-international,基本的SpringBoot+thymeleaf+国际化信息(message.properties)项目
1.2 maven配置
添加thymeleaf依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.yisu.cloud</groupId>
<artifactId>fw-cloud-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
1.3 国际化配置
设置了一个localeResolver,可以采用Cookie来控制国际化的语言,也可以采用Session来控制,两个启用一个即可。还设置一个LocaleChangeInterceptor拦截器来拦截国际化语言的变化,并且将拦截器加入到Spring中。
@Configuration
public class I18nConfig extends WebMvcConfigurationSupport {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.setDefaultLocale(Locale.CHINA);
return resolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
1.4 控制层
对于使用thymeleaf的我们可以直接跳转到页面,使用方式和JSP类似。这里我们设置默认页面就是跳转到index.html
@Controller
public class IndexController {
@GetMapping("/")
public String index() {
return "/index";
}
}
1.5 message 信息
中文zh_CN
login.userId=用户名
login.noUserId=请输入用户名
login.password=密码
login.noPassword=密码不能为空
login.login=登录
英文en_US
login.userId = Login ID
login.noUserId = Please enter the user ID
login.password = Password
login.noPassword = password can not be blank
login.login = Login
1.6 页面
模拟一个简单的表单登录
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot-international</title>
</head>
<body>
<div>
<form th:align="center">
<label th:text="#{login.userId}">Username</label>
<input type="text" th:placeholder="#{login.noUserId}" required="" autofocus="">
<br>
<label th:text="#{login.password}">Password</label>
<input type="password" th:placeholder="#{login.noPassword}" required="">
<br>
<button type="submit" th:text="#{login.login}">Sign in</button>
</form>
</div>
</body>
</html>
1.7 应用启动并访问
浏览器输入http://localhost:8774/,可以看到如下表单,默认是中文的,所以他默认会去messages_zh_CN.properties中找,如果没有就会去messages.properties中找。
如果输入http://localhost:8774/?locale=en_US语言就会切到英文。同样的如果url后参数设置为locale=zh_CH,语言就会切到中文
1.8 前后端分离的情况
对于如果不是thymeleaf的环境,而是前后端分离的情况,可以使用如下方式,通过接口设置语言环境,默认中文,然后通过key 获取对应的value值。
@RestController
public class LanguageController {
@Autowired
private MessageUtil messageUtil;
@GetMapping("/setLang")
public FwResult getInfoByLang(HttpServletRequest request, HttpServletResponse response,
String lang){
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if("zh".equals(lang)){
localeResolver.setLocale(request, response, new Locale("zh", "CN"));
}else if("en".equals(lang)){
localeResolver.setLocale(request, response, new Locale("en", "US"));
}
return FwResult.okMsg("设置"+lang+"成功");
}
@GetMapping("/getValue")
public FwResult getValue(String key) {
String welcome = messageUtil.getMessage(key);
return FwResult.ok(welcome);
}
}
获取message 中的国际化配置信息,这里抽取成一个公共方法
@Component
public class MessageUtil {
@Resource
private MessageSource messageSource;
public String getMessage(String code) {
return getMessage(code, null);
}
public String getMessage(String code, Object[] args){
return getMessage(code, args, "");
}
public String getMessage(String code,Object[] args,String defaultMessage){
Locale locale = LocaleContextHolder.getLocale();
return messageSource.getMessage(code, args, defaultMessage, locale);
}
}
1.9 启动应用测试
浏览器或Postman 输入localhost:8774/getValue?key=login.noUserId
修改语言环境localhost:8774/setLang?lang=en
浏览器或Postman 再次输入localhost:8774/getValue?key=login.noUserId
1.10 乱码处理
如果遇到国际化配置文件中存在乱码的情况可以按照下图将标记的部分勾选即可






