pageContext.response.locale is always set to en_US even if browser has ja as first language

StackOverflow https://stackoverflow.com/questions/22685528

  •  22-06-2023
  •  | 
  •  

My application is running on tomcat7 and supports both ja and en locale.

I have a login.jsp file that I would like to set the locale and bundle by this code:
<fmt:setLocale value="${pageContext.response.locale}" scope="session" /> <fmt:setBundle basename="com.application.i18n.labels" scope="session" />

I have my chrome browser set with the first language as Japanese and second language as English: Accept-Language:ja,en;q=0.8

However, whenever the login.jsp is loaded it always displays the login.jsp as en_US. I tried to display the content of pageContext.response.locale and the value is confirmed en_US. The content-language header for the displayed jsp also confirms this: Content-Language:en-US

When I explicitly set the <fmt:setLocale value="ja" scope="session" /> the login.jsp is able to properly display in Japanese. But I do not want to force the login.jsp to always be set to Japanese. I want to have the login.jsp displayed depending on the first supported Accept-language value, which in my browser's case is supposed to be ja.

How can I display the login.jsp based on the first supported Accept-Language by the application?

Edit: I experimented by using <%=request.getLocale()%> and it displayed ja as the locale. This got me confused. Isn't it that pageContext.response.locale is supposedly set with the value coming from the request locale?

有帮助吗?

解决方案

Servlet specification 3.1, section 5.5 "Servlets should set the locale and the character encoding of a response." so this is an application responsibility.

I don't see anything in the Servlet or JSP specifications that requires that Response Locale to be set based on the Request Locale.

其他提示

In response to Mark Thomas's comment, the render() method of spring's DispatcherServlet.java sets the locale of the response from the request using a resolution mechanism:

// Determine locale for request and apply it to the response.
Locale locale = this.localeResolver.resolveLocale(request);
response.setLocale(locale);

The LocaleResolver defaults to AcceptHeaderLocaleResolver which simply returns the request locale as follows:

public Locale resolveLocale(HttpServletRequest request) {
    return request.getLocale();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top