使用Java过滤器更改区域设置不起作用

Ily*_*tel 0 java servlet-filters

我正在尝试使用Java Filter更改Locale,但以下代码不起作用,因为JSP页面仍以英语呈现:

public class PreferenceFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        Locale locale = StringUtils.parseLocaleString("fr"); 

        res.setLocale(locale);
        chain.doFilter(req, res);
    }
}
Run Code Online (Sandbox Code Playgroud)

我也在使用Spring MVC,我们正在使用自己的翻译系统来获取不同语言环境的翻译:

<bean id="messageSource"
    class="com.mycompany.web.translations.DatabaseDrivenMessageSourceImpl" scope="singleton">
    <property name="cacheSeconds" value="3"/>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

JB *_*zet 5

如果您正在使用JSTL来国际化您的JSP,这是正常的:JSTL不会从响应中获取区域设置,而是从配置的范围参数获取,或者,如果未设置区域设置,则从请求中获取.

使用

Config.set(request, Config.FMT_LOCALE, locale);
Run Code Online (Sandbox Code Playgroud)

其中Config是班级javax.servlet.jsp.jstl.core.Config..