SpringBoot internationalization configuration, SpringBoot Locale internationalization
================================
©Copyright Sweet Potato Yao March 27, 2018
http://fanshuyao.iteye.com/
1. The effect is as follows:
2. SpringBoot internationalization configuration
1. Create internationalization configuration files (3):
mess.properties
mess.user.name=Username mess.user.password=Password mess.user.btn=Login
mess_zh_CN.properties
mess.user.name=Username mess.user.password=Password mess.user.btn=Login
mess_en_US.properties
mess.user.name=UserName mess.user.password=Password mess.user.btn=Sign In
The default internationalization file of SpringBoot is: classpath:message.properties. If it is placed in another folder, you need to configure the property spring.messages.basename in application.properties:
# Indicates the i18n folder placed on the classpath, the file prefix is mess spring.messages.basename=i18n.mess
2. Custom internationalized language parser
import java.util.Locale; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.web.servlet.LocaleResolver; import org.thymeleaf.util.StringUtils; /** * Custom internationalized language parser * */ public class MyLocaleResolver implements LocaleResolver{ private static final String I18N_LANGUAGE = "i18n_language"; private static final String I18N_LANGUAGE_SESSION = "i18n_language_session"; @Override public Locale resolveLocale(HttpServletRequest req) { String i18n_language = req.getParameter(I18N_LANGUAGE); Locale locale = Locale.getDefault(); if(!StringUtils.isEmpty(i18n_language)) { String[] language = i18n_language.split("_"); locale = new Locale(language[0], language[1]); //将国际化语言保存到session HttpSession session = req.getSession(); session.setAttribute(I18N_LANGUAGE_SESSION, locale); }else { //如果没有带国际化参数,则判断session有没有保存,有保存,则使用保存的,也就是之前设置的,避免之后的请求不带国际化参数造成语言显示不对 HttpSession session = req.getSession(); Locale localeInSession = (Locale) session.getAttribute(I18N_LANGUAGE_SESSION); if(localeInSession != null) { locale = localeInSession; } } return locale; } @Override public void setLocale(HttpServletRequest req, HttpServletResponse res, Locale locale) { } }
3、把国际化语言解析器放到Spring容器中:
这里创建了一个自定义的配置类:CustomMvcConfig ,继承WebMvcConfigurerAdapter,可以扩展SpringMvc的功能,包括拦截器,转换器等
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.lqy.springboot.message.locale.MyLocaleResolver; //使用WebMvcConfigurerAdapter可以扩展SpringMvc的功能,包括拦截器,转换器等 //@EnableWebMvc //设置@EnableWebMvc为完全接管SpringMvc,但一般不要设置完全接管SpringMvc @Configuration public class CustomMvcConfig extends WebMvcConfigurerAdapter { /** * 配置自己的国际化语言解析器 * @return */ @Bean public LocaleResolver localeResolver() { return new MyLocaleResolver(); } /** * 配置自己的拦截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { //super.addInterceptors(registry); } }
4、页面显示及切换国际化操作:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> .ib{ display: inline-block; } .ml20{ margin-left: 20px; } .mt20{ margin-top: 20px; } </style> </head> <body> <div> <div>[[#{mess.user.name}]]:<input th:placeholder="#{mess.user.name}"/></div> </div> <div> <div>[[#{mess.user.password}]]:<input th:placeholder="#{mess.user.password}"/></div> </div> <div> <div><button>[[#{mess.user.btn}]]</button></div> </div> <div class="mt20"> <span class="ib"><a th:href="@{/mess(i18n_language=zh_CN)}">中文</a></span> <span class="ib ml20"><a th:href="@{/mess(i18n_language=en_US)}">英文</a></span> </div> </body> </html>
注:附件有源码
================================
©Copyright 蕃薯耀 2018年3月27日
http://fanshuyao.iteye.com/