Question

I have 4 available locales in my project: ar_AE, en_GB, en_US, ur_PK. The default set up in my localeResolver is en_GB.

No matter what I set my current locale to, when I enter a webflow, the EL expression ${pageContext.response.locale} always evaluates to en_US, but the messages are looked up correctly (so, for example, if I'm currently ar_AE, the text is arabic, the text direction is rtl, but the current language in my JSP is en_US).

My locale setup:

<mvc:interceptors>
    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>
</mvc:interceptors>

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="en_GB" />
</bean>

My JSP snippet:

<a href="#i18n" class="dropdown-toggle" data-toggle="dropdown">
    <span class="${pageContext.response.locale}-i18n-option i18n-option-selected"> 
        <spring:message code="Native Name (${pageContext.response.locale})" /> 
    </span>
    <b class="caret"></b>
</a>

My webflow config:

<!--Maps request paths to flows in the flowRegistry--> 
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping"> 
    <property name="order" value="0" /> 
    <property name="flowRegistry" ref="flowRegistry" /> 
</bean>

<!-- Dispatches requests mapped to flows to FlowHandler implementations -->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
    <property name="flowExecutor" ref="flowExecutor" />
</bean>

<!-- Executes flows: the entry point into the Spring Web Flow system -->
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry" />

<!-- The registry of executable flow definitions -->
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" base-path="/WEB-INF/webflows">
    <webflow:flow-location-pattern value="/**/flow.xml" />
</webflow:flow-registry>

<!-- Plugs in a custom creator for Web Flow views -->
<webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator" development="true" />

<bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
    <property name="viewResolvers" ref="viewResolver" />
</bean>

So how do I get the correct current locale to display in my JSP within the webflow?

Was it helpful?

Solution

The problem is apparently that the PageContext hasn't changed for the web flow, and I need to explicitly set the context attribute in my viewResolver bean:

<bean 
    id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver" 
    p:requestContextAttribute="requestContext" 
    p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView" />

Then I can change the line in my JSP to read:

<span class="${requestContext.locale}-i18n-option i18n-option-selected"> 
    <spring:message code="Native Name (${requestContext.locale})" /> 
</span>

Which reads from the new context attribute requestContext. Since I've used the viewResolver bean for both web flows and non-web-flow pages, this solution works globally.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top