SpringMVC国际化设置

xiaoxiao2021-02-28  79

通过Http 请求头

使用gradle构建项目:http://blog.csdn.net/x_iya/article/details/64442112

1、创建国际化资源文件。

需要先做如下设置:http://www.cnblogs.com/galaxy-gao/p/4616313.html

在resources目录下右键NEW-->Resource Bundle

然后再如下创建两个国际化资源文件。

2、编写国际化资源文件。

如上点击加号创建属性。

3、在springMVC配置文件中配置messageSource

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="i18n" /> </bean>

@Controller public class LanguageController { @Autowired private ResourceBundleMessageSource messageSource; @RequestMapping("/i18n") public String testI18n(Locale locale) { String val = messageSource.getMessage("username", null, locale); System.out.println(val); return "i18n"; } }

i18n.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>i18n</title> </head> <body> <fmt:message key="username"/> <br /> <fmt:message key="password"/> </body> </html>

测试:

curl -H "Accept-Language: en-US" http://localhost:8080/International/i18n

curl -H "Accept-Language: zh-CN" http://localhost:8080/International/i18n

记录一个bug

我在首页中直接使用 <fmt:message> 标签,出现不能识别该标签的错误。

因为在web.xml中配置的DispatcherServlet的url-pattern为“/”,所以不会匹配访问.jsp的url,所以直接访问首页并不会经过DispatcherServlet,导致无法读取到资源文件

解决:

<fmt:bundle basename="i18n"> <fmt:message key="hello"/> </fmt:bundle>

通过参数

通过参数来控制网站的本地化,如:www.xx.com?lang=en、www.xx.com?lang=zh。

这一需求可以通过LocaleChangeInterceptor过滤器来实现。

<mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="lang"/> </bean> </mvc:interceptors> LocaleChangeInterceptor的主要任务是从请求中获取本地化类型并将其设置给真正的本地化解析器,所以在此之前需要配置本地化解析器。

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>

public class LocaleChangeInterceptor extends HandlerInterceptorAdapter { //... @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; } //... }核心的代码:

String newLocale = request.getParameter(getParamName());//获得区域请求参数

LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);//获得区域解析器

public static LocaleResolver getLocaleResolver(HttpServletRequest request) { return (LocaleResolver) request.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE); } 在DispatcherServlet中由如下的代码

request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);

localeResolver.setLocale(request, response, parseLocaleValue(newLocale));//保存区域设置

转载请注明原文地址: https://www.6miu.com/read-78185.html

最新回复(0)