Search code examples
jsplocale

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


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?


Solution

  • 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.