在日常开发中,如果涉及到网站需要多语言显示的话,那么利用Spring Boot要怎么做呢?又涉及到了哪些内容呢,下面是我开发中用的的一些记录。仅供参考。
我这边需要的语言有,假定需要的语言有:中文zh,英文en
使用到的主要技术有:SpringBoot,Thymeleaf,
关键的类:CookieLocaleResolver,LocaleChangeInterceptor,WebMvcConfigurerAdapter的addInterceptors
另外很多内容都是参照: http://412887952-qq-com.iteye.com/blog/2312274
这个关于SpringBoot讲解的很全面了。
1、首先我们先定义国际化资源文件,spring boot默认就支持国际化的,而且不需要你过多的做什么配置,只需要在resources/下定义国际化配置文件即可
如果想修改文件的位置,可以在application.properties中定义
spring.messages.basename=message/messages
那么messages_en.properties,messages_zh.properties 就可以放到resources/message文件夹下了
上面链接的博客中是使用
@Configuration public class WebConfiguration extends WebMvcConfigurerAdapter { @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("message/messages"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } }
@Autowired protected MessageSource messageSource; String msg = messageSource.getMessage("welcome", null,locale);
这种形式,然后put到前台,但是这种如果前台全是静态的页面,都通过put形式未免太麻烦了。有没有什么其他方式呢?
thymeleaf支持#{welcome}形式读取message信息。
那么就可以使用LocaleChangeInterceptor 拦截器,来拦截语言的变化,然后CookieLocaleResolver设置cookie,前段也自然就跟着变化了。
@Configuration public class WebConfiguration extends WebMvcConfigurerAdapter { @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); lci.setParamName("lang"); return lci; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); } }
这样,就可以捕获浏览器地址栏lang参数的变化了
http://localhost:8080/index?lang=zh
http://localhost:8080/index?lang=en
通过查看拦截器的源码可以看到:
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException { String newLocale = request.getParameter(getParamName()); if (newLocale != null) { if (checkHttpMethod(request.getMethod())) { LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); if (localeResolver == null) { throw new IllegalStateException( "No LocaleResolver found: not in a DispatcherServlet request?"); } try { localeResolver.setLocale(request, response, parseLocaleValue(newLocale)); } catch (IllegalArgumentException ex) { if (isIgnoreInvalidLocale()) { logger.debug("Ignoring invalid locale value [" + newLocale + "]: " + ex.getMessage()); } else { throw ex; } } } } // Proceed in any case. return true; }
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); 这句如果没有特意指定localeResolver 的话,在执行过程中会报错:
java.lang.UnsupportedOperationException:
Cannot change HTTP accept header - use a different locale resolution strategy
...AcceptHeaderLocaleResolver.setLocale(AcceptHeaderLocaleResolver.java:45)
原因是:
In Spring MVC application, if you do not configure the Spring’s LocaleResolver, it will use the default AcceptHeaderLocaleResolver, which does not allow to change the locale. To solve it, try declare a SessionLocaleResolver bean in the Spring bean configuration file, it should be suits in most cases.
<beans ...
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language" />
</bean>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
</beans>
咱们使用的是SpringBoot,所以可以通过注解@Bean的形式注入一个localeResolver
@Configuration public class WebConfiguration extends WebMvcConfigurerAdapter { @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); lci.setParamName("lang"); return lci; } @Bean public LocaleResolver localeResolver() { CookieLocaleResolver cl = new CookieLocaleResolver(); cl.setCookieName("language"); return cl; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); } }
此处用的是Cookie记录,所以注入Cookie,当然也可以Session什么的,自己随意。
通过以上,就可以通过在浏览器lang的切换,然后页面显示不同配置的语言了。
但是现在开发中语言设置一般遵循restful的风格,即访问地址为:
http://localhost:8080/en/
http://localhost:8080/zh/
这种形式,这样LocaleChangeInterceptorgetParameter就获取不到lang了。
所以就需要自己重新写一个拦截器,只需要继承LocaleChangeInterceptor,然后覆写preHandle即可。
@Component public class LanguageInterceptor extends LocaleChangeInterceptor { @Autowired private LocaleResolver localeResolver; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException { //获取 @pathvariable 的参数 /{lang} Map map = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); String lang = MapUtils.getString(map, "lang", ""); Locale l = new Locale(lang); localeResolver.setLocale(request, response, l); } }
只要在localeResolver中设置了Locale,那么就是按照Locale的语言进行显示了。
然后修改addInterceptors
@Configuration public class WebConfiguration extends WebMvcConfigurerAdapter { @Autowired private LanguageInterceptor languageInterceptor; @Bean public LocaleResolver localeResolver() { CookieLocaleResolver cl = new CookieLocaleResolver(); cl.setCookieName("language"); return cl; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(languageInterceptor); } }
相关推荐
毕业设计,基于SpringBoot+Thymeleaf+MySql开发的智慧高校学术报告系统,内含Java完整源代码,数据库脚本 智慧高校学术报告系统(academic-report)是一个基于 SpringBoot 开发的标准 Java Web 项目。**系统整体...
前端技术: HTML:用于定义网页结构的标记语言。 CSS :用于设计网页外观和样式的样式表...Python:一种多用途编程语言,在Web开发中常用。 Ruby on Rails:一个基于Ruby编程语言的Web应用框架,提供了高效的开发工具。
系统采用的开发语言是java语言,因此运行此系统的环境搭建为java开发环境和开发平台即可。 数据库采用的是MySQL,轻量级易安装。系统内含相应的SQL脚本文件,只要运行即可获得相应数据库,无需手动创建。 建议安装...
本程序采用 SpringBoot+Mysql+MyBatis+Thymeleaf+Amaze UI+ECharts 等框架技术支持。 友情提示:[hutool ](https://git.oschina.net/loolly/hutool) 国产最佳Java 工具类! BabyLog是用来记录孩子成长过程的日记,...
基于SpringBoot + Mybatis + Thymeleaf + Redis + MongoDB + MySQL开发的 MySQL 是一款广受欢迎的开源关系型数据库管理系统(RDBMS),由瑞典MySQL AB公司开发,现隶属于美国甲骨文公司(Oracle)。自1998年首次...
它完全支持结构化查询语言(SQL),允许用户进行数据查询、插入、更新、删除、创建和管理数据库结构等操作。SQL标准的广泛支持使得MySQL易于学习,且与其他关系型数据库系统有良好的互操作性。 存储引擎 MySQL支持...
springboot停车位管理系统源码 开发语言 : JAVA 数据库 : MySQL 开发工具 : Eclipse 源码类型 : WebForm 注意:不带技术支持,有...使用技术: spring+spring mvc+mybatis+bootstrap+jquery+html+springboot+thymeleaf
它完全支持结构化查询语言(SQL),允许用户进行数据查询、插入、更新、删除、创建和管理数据库结构等操作。SQL标准的广泛支持使得MySQL易于学习,且与其他关系型数据库系统有良好的互操作性。 存储引擎 MySQL支持...
使用技术: springboot+Thymeleaf+mybatis 2.小程序API,目前API模块已经基本完成, 涉及到技术: springboot+mybatis-plus+jwt+mapstruct+lombok+redis+swagger 3.mpvue小程序,小程序主要是用 mpvue 框架开发
一款 Java 语言基于 SpringBoot2.x、Layui、Thymeleaf、MybatisPlus、Shiro、MySQL等框架精心打造的一款模块化、插件化、高性能的前后端分离架构敏捷开发框架,可用于快速搭建前后端分离后台管理系统,本着简化开发...
项目描述 在上家公司自己集成的一套系统,用了两个多月的时间完成的:Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级开发系统 Springboot作为容器,使用mybatis作为持久层框架 使用官方推荐的thymeleaf做为...
在上家公司自己集成的一套系统,用了两个多月的时间完成的:Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级开发系统 Springboot作为容器,使用mybatis作为持久层框架 使用官方推荐的thymeleaf做为模板引擎...
3. 视图解析器采用了thymeleaf 4. 前段UI框架采用BootStrap4.0.0, 配合Layui UI经典模块化前端框架 5. 在线播放器采用CkPlayer6.7 6. 百度多平台分享插件 数据库文件 链接:...
后台管理模块用于银行工作人员对本系统业务处理过程的管理与监控,前端采用 SpringBoot默认支持的Thymeleaf模板引擎渲染页面,后端通过Shiro进行身份权限 的认证,采取Restfill风格实现定时任务、日志的管理以及...
基于SpringBoot 2.x版本, 目前相对比较全的SpringBoot教学视频,以通俗易懂的方式讲解SpringBoot核心技术, 适合初学者的教程,让你少走弯路! 课程内容包括: 1.SpringBoot简介、优点 2.SpringBoot应用 3.配置...
基于Java语言开发,使用Spring Boot作为主要框架,这个系统旨在简化公寓的日常管理工作,提供一个用户友好的界面,以及高效的数据处理能力。该系统集成了多项功能,包括住户信息管理、费用收缴、维修管理、活动组织...
本毕业设计选用SpringBoot框架,结合Thymeleaf,SpringData,SpringSecurity,Elasticsearch等技术,旨在为技术人员设计并实现一款用于记录并分享技术文档的技术博客。通过该技术博客,方便技术人员记录自己工作和...
一款 Java 语言基于 SpringBoot2.x、Layui、Thymeleaf、MybatisPlus、Shiro、MySQL等框架精心打造的一款模块化、插件化、高性能的前后端分离架构敏捷开发框架,可用于快速搭建前后端分离后台管理系统,本着简化开发...
一款 Java 语言基于SpringBoot2.x、Layui、Thymeleaf、MybatisPlus、Shiro、MySQL等框架精心打造的一款模块化、插件化、高性能的前后端分离架构敏捷开发框架,可用于快速搭建后台管理系统,本着简化开发、提升开发...