Springboot根据浏览器实现网站资源国际化

时间:2023-03-08 17:18:18

每天学习一点点 编程PDF电子书、视频教程免费下载:
http://www.shitanlife.com/code

根据浏览器地区主动选择资源

1.创建资源化文件
  • 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)
2.配置application.properties
   spring.messages.encoding=UTF-8
spring.messages.basename=/messages/messages
3.jsp页面中使用标签
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<title>i18n</title>
</head>
<body>
<spring:message code="welcome"></spring:message>
</body>
</html>
4.修改浏览器的语言环境
  • 浏览器地址栏中输入about:config
  • 修改intl.accept_languages,查看结果
  • 以上为火狐浏览器

页面链接中实现对资源化得控制

如同以下页面,点击相关链接完成网页资源切换
Springboot根据浏览器实现网站资源国际化

1.创建MyLocaleResolver类集成AcceptHeaderLocaleResolver类
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;
}
}
2.创建I18nConfig类初始化localeResolver对象bean

对象的名称必须为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;
}
3.LoginControl类中代码实现跳转
  • 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");
}
4.welcome.jsp页面代码
<%@ 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