raq
raq

Reputation: 175

how do you read locale information from session?

The following bit of code sets the Locale from lang param on querystring.

<c:if test="${param['lang'] != null}">
    <fmt:setLocale value="${param['lang']}" scope="session"/>
</c:if>

How do I now read that variable?

I know its a bit noobish.

I need a bit of conditional logic to display one language link if the local hasn't been set in the session scope using the fmt:setLocale, and another if it has been set to a specific locale.

Thanks

Upvotes: 2

Views: 10261

Answers (3)

raq
raq

Reputation: 175

<c:choose>
    <c:when test="${sessionScope['javax.servlet.jsp.jstl.fmt.locale.session'] eq 'en_CY'}">
        a href="?lang=en_GB">English</a>
    </c:when>
    <c:otherwise>
        <a href="?lang=en_CY">Cymraeg</a>
    </c:otherwise>
</c:choose>

this works but is there a better way to write?

<c:when test="${sessionScope['javax.servlet.jsp.jstl.fmt.locale.session'] eq 'en_CY'}">

Upvotes: 2

raq
raq

Reputation: 175

<c:choose>
    <c:when test="${pageContext.response.locale eq 'en_CY'}">
        <a href="?lang=en_GB">English</a>
    </c:when>
    <c:otherwise>
        <a href="?lang=en_CY">Cymraeg</a>
    </c:otherwise>
</c:choose>

this works only on the page itslef.

but because its reading from the pageContext, it will not work on other pages by reading it from the sessionScope (where it is being set by the fmt:setLocale).

How would i read it from the sessionScope?

Upvotes: 0

Chris Thornhill
Chris Thornhill

Reputation: 5431

Try this:

<c:if test="${pageContext.request.locale.language == 'en'}"> 
    <a href="link1">Link 1</a> 
</c:if> 
<c:if test="${pageContext.request.locale.language != 'en'}"> 
    <a href="link2">Link 2</a> 
</c:if> 

Upvotes: 5

Related Questions