Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display the locale in a JSTL

In a jstl page i want to display the current locale being used to render the page. The page locale is working just fine. When i switch the locale (?lang=nl) the nl resource bundle is used. When is switch back to english (?lang=en) the en resouce bundle is used.

Now i try to display the locale using

<c:out value="${ PageContext.request.locale.language }"/>

But this just displays the literal string ${ PageContext.request.locale.language }

I have tried a lot of samples like. With and without the spaces.

${ pageContext.request.locale.language }
${ pageContext.locale }
${ pageContext.getLocale }
${ requestContext.locale.language }
${ request.locale.language }

I'm using the spring-mvc with the following configuration

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

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="en"/>
</bean>

The code for reproducing this problem can be found on https://github.com/abroer/jsltSpringLocaleProblem

Could anyone help me out on this one. Thanks for taking the effort. I really appriciate it.

like image 522
DukeMe Avatar asked Dec 25 '22 15:12

DukeMe


1 Answers

I tried with your example from github and it seems to work for me with the expression -

<c:out value="${pageContext.request.locale.language}"/> 

The only thing I had to change was the webapp namespace and schema to 2.5 as opposed to version 2.3 in your web.xml file as I'm using jdk 7 with tomcat 7.

like image 134
Ivey Avatar answered Jan 09 '23 03:01

Ivey