标签:control cep stl text tostring 配置 har over default
每天学习一点点 编程PDF电子书、视频教程免费下载:
http://www.shitanlife.com/code
- resource目录下创建messages目录
- 创建messages_en_US.properties、messages_zh_CN.properties文件。分别是英文、中文资源。
- messages.properties文件为默认文件。
- messages_en_US.properties写入内容:welcome = welcome to login in soa-watch systerm(english)
- messages_zh_CN.properties写入内容:welcome=欢迎访问soa-watch系统(CH)
- messages.properties写入内容:welcome=欢迎访问soa-watch系统(Default)
spring.messages.encoding=UTF-8
spring.messages.basename=/messages/messages
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<title>i18n</title>
</head>
<body>
<spring:message code="welcome"></spring:message>
</body>
</html>
- 浏览器地址栏中输入
about:config
- 修改intl.accept_languages,查看结果
- 以上为火狐浏览器
如同以下页面,点击相关链接完成网页资源切换
public class MyLocaleResolver extends AcceptHeaderLocaleResolver {
private Locale myLocal;
@Override
public Locale resolveLocale(HttpServletRequest request) {
return myLocal==null?request.getLocale():myLocal;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
myLocal = locale;
}
}
对象的名称必须为localeResolver,Spring容器会自动加载,否则找不到。
@Configuration
public class I18nConfig {
private static Logger logger = LoggerFactory.getLogger(I18nConfig.class);
@Bean(name = "localeResolver")
public MyLocaleResolver myLocaleResolver(){
logger.info("#####cookieLocaleResolver---create");
MyLocaleResolver myLocaleResolver = new MyLocaleResolver();
myLocaleResolver.setDefaultLocale(Locale.ENGLISH);
logger.info("#####cookieLocaleResolver:");
return myLocaleResolver;
}
- welcome页面为welcome.jsp,展示页面
- language请求为动态设置语言环境
@GetMapping("/welcome")
public String welcome(){
logger.info("class:"+messageSource.getClass());
return "welcome";
}
@GetMapping("/language")
public ModelAndView language(HttpServletRequest request, HttpServletResponse response, String language){
Locale locale= request.getLocale();
logger.error(locale.toString());
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
language=language.toLowerCase();
logger.info("language:"+language);
if(language==null||language.equals("")){
return new ModelAndView("welcome");
}else{
if(language.equals("zh_cn")){
localeResolver.setLocale(request, response, Locale.CHINA);
}else if(language.equals("en")){
localeResolver.setLocale(request, response, Locale.US);
}else{
localeResolver.setLocale(request, response, Locale.CHINA );
}
}
return new ModelAndView("redirect:welcome");
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<title>i18n</title>
</head>
<body>
<spring:message code="welcome"></spring:message>
<a href="<c:url value=‘language?language=zh_cn‘/>">
中文
</a>
<a href="<c:url value=‘language?language=en‘/>">
english
</a>
</body>
</html>
实现完成,即可测试。
每天学习一点点 编程PDF电子书、视频教程免费下载:
http://www.shitanlife.com/code
标签:control cep stl text tostring 配置 har over default
原文地址:https://www.cnblogs.com/scode2/p/9050752.html