注意国际化文件必须在resource下,且文件后缀形如_zh_CN.properties
这是springboot解析规范,当然本章讲解ssm国际化.springboot更为简单.
xml引入识别国际化资源
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n.language" />
<property name="useCodeAsDefaultMessage" value="true" />
<property name="defaultEncoding" value="utf-8"/>
bean>
<mvc:interceptors>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
bean>
mvc:interceptors>
MyLocalResolver.java
@Configuration
public class MyLocalResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
//获取区域的标识
String flag = httpServletRequest.getParameter("flag");
//获取默认区域对象
Locale locale = Locale.getDefault();
//判断区域标识是否为空
if(flag != null){
//对区域标识进行拆分处理
String[] splits = flag.split("_");//使用下划线切分字符串返回一个字符串数组
//利用区域标识创建区域对象
locale = new Locale(splits[0],splits[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocalResolver();
}
}
MyLocalResolver配置类加了@Configuration注解,因此请求会被MyLocalResolver 处理切换语言
@{/}默认被解析跳转到login