1. 在页面上能够根据浏览器语言设置的情况对文本(不是内容), 时间, 数值进行本地化处理
2. 可以在 bean 中获取国际化资源文件 Locale 对应的消息
3. 可以通过超链接切换 Locale, 而不再依赖于浏览器的语言设置情况
解决:
- 使用 JSTL 的 fmt 标签
<fmt:message key="i18n.username"/>
<br>
<fmt:message key="i18n.password"/>
- 在 bean 中注入 ResourceBundleMessageSource 的示例, 使用其对应的 getMessage 方法即可
@RequestMapping("/i18n1")
public String testi18n(Locale locale){
String val = messageSource.getMessage("i18n.username",null,locale);
System.out.println(val);
return "i18n1";
}
- 配置 LocalResolver 和 LocaleChangeInterceptor
<!--配置SessionLocaleResolver-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
<!--配置LocaleChangeInterceptor-->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>
<fmt:message key="i18n.username"/>
<br>
<fmt:message key="i18n.password"/>
<br><br>
<a href="i18n1?locale=zh_CH">中文</a>
<br>
<a href="i18n1?locale=en_US">英文</a>
LocalResolver 和 LocaleChangeInterceptor的工作原理