Sergio del Amo
Sergio del Amo

Reputation: 78126

How do I access Locale from a JSP?

I want to include a js file depending on the value of the current Locale. I have tried to access it from JSP as follows:

<%@ page import="java.util.Locale" %>  
<% if( ((Locale) pageContext.getAttribute("org.apache.struts.action.LOCALE",PageContext.REQUEST_SCOPE)).getLanguage().equals("de")) { %>
    <script src="../themes/administration/js/languages/i18nDE.js" type="text/javascript"> </script>
<% } else { %>
    <script src="../themes/administration/js/languages/i18nEN.js" type="text/javascript"> </script>
<% } %>

However, I am getting a java.lang.NullPointerException because pageContext.getAttribute("org.apache.struts.action.LOCALE",PageContext.REQUEST_SCOPE) is NULL.

Does anyone knows how I can solve this?

Upvotes: 15

Views: 46571

Answers (10)

dawez
dawez

Reputation: 2724

At the moment I am using this :

<c:set var="localeCode" value="${pageContext.response.locale}" />

This can later be access by using ${localeCode}

  1. Scriplet mode, discouraged! See Why not use Scriptlets for reason not to use a scriptlet.

The localeCode variable can be queried inside a scriptlet with:

<%
  Object ob_localeCode = pageContext.getAttribute("localeCode");
  if (ob_localeCode != null) {
    String currentLanguageCode = (String) ob_localeCode;
  }
  //more code
%>
  1. Scripletless mode correct way to go. See How to avoid Java Code in JSP-Files? Here on SO.

I am using spring 2.5 config at the moment.

So following this, coming back to your original question you can implement something like:

<c:set var="localeCode" value="${pageContext.response.locale}" />
<c:choose>
  <c:when test="$localecode == 'de' }"> 
    <script src="../themes/administration/js/languages/i18nDE.js" type="text/javascript"> </script>
  </c:when>
  <c:otherwise>
    <script src="../themes/administration/js/languages/i18nEN.js" type="text/javascript"> </script>
  </c:otherwise>
</c:choose>

or if you really want to use some short code to impress your colleagues, you can do:

<c:set var="localeCode" value="${fn:toUpperCase(pageContext.response.locale)}" />
<c:set var="availLanguages" value="EN,DE" />
<c:if test="${!fn:contains(availLanguages,localeCode)}">
  <c:set var="localeCode" value="EN" />
</c:if>

<script src="../themes/administration/js/languages/i18n{$localeCode}.js" type="text/javascript"> </script>

Upvotes: 20

Thilina Rubasingha
Thilina Rubasingha

Reputation: 1089

Try with this

<s:if test='locale.toString() == "si"'>
    <script src="../themes/administration/js/languages/i18nDE.js" type="text/javascript"> </script>
</s:if>
<s:elseif test='locale.toString() == "ta"'>
    <script src="../themes/administration/js/languages/i18nEN.js" type="text/javascript"> </script>
</s:elseif>
<s:else>
    ANOTHER SCRIPT
</s:else>

Upvotes: 1

PbxMan
PbxMan

Reputation: 7623

I added new examples to clarify this a bit more because this post didn't help me much.

To get locale from jsp:

<%=request.getLocale()%>

it's a ServletRequest Method a Returns the preferred Locale that the client will accept content in, based on the Accept-Language header,

Struts2 Locale: <s:property value="#request.locale"/>

Returns the locale for the Struts2 Framework, that may or may not be the same as in the previous example. if you pass the param request_locale=de for instance...

<s:url id="localeDE" namespace="/">
   <s:param name="request_locale" >de</s:param>
</s:url>
<s:a href="%{localeDE}" >German</s:a>

the struts2 #request.locale will changed to german overriding the value of the original Accept-Language header

Upvotes: 1

jeremy
jeremy

Reputation: 1

The two best ways to get locale is by using the getLocale of Action support inherited by an action, onto a JSP: <s:hidden name="locale"/> or
<s:property value"%{locale}"/>

When locale has been changed with this method.

It is not the same as:
${pageContext.response.locale}

Upvotes: 0

ravi
ravi

Reputation:

<%@page import="java.util.Locale"%>
<%@page import="org.apache.struts.Globals"%>


<%Locale locale = (Locale)session.getAttribute(Globals.LOCALE_KEY);
if (locale.getLanguage().equals("fr")) {%>
    <script language="JavaScript" src="lib/js/dateofday.js" type="text/javascript"></script>
    <script type="text/javascript" src="<%=request.getContextPath() %>/lib/js/jscalendar-1.0/lang/calendar-fr.js"></script>
<%} else {%>
    <script language="JavaScript" src="lib/js/dateofday-en.js" type="text/javascript"></script>
    <script type="text/javascript" src="<%=request.getContextPath() %>/lib/js/jscalendar-1.0/lang/calendar-en.js"></script>
<%}%>

Upvotes: 0

sunil
sunil

Reputation: 71

in Struts2 try

<s:if test="#request.locale.language=='us'">
     <s:select name="gender" list="#{'M':'Male','F':'female'}" ></s:select>
 </s:if>

Upvotes: 6

Carles Barrob&#233;s
Carles Barrob&#233;s

Reputation: 11693

In Struts2, using EL I successfully used:

${sessionScope["org.apache.struts2.action.LOCALE"]}

E.g. to output the value of the Locale:

<c:out value='${sessionScope["org.apache.struts2.action.LOCALE"]}'/>

Upvotes: 1

Faisal Feroz
Faisal Feroz

Reputation: 12785

Struts puts locale in the session. The correct way to get the Locale is:

Locale locale = (locale)request.getSession().getAttribute(Globals.LOCALE_KEY);

Upvotes: 3

Sergio del Amo
Sergio del Amo

Reputation: 78126

Ken G. pointed to the answer.

pageContext.getAttribute("org.apache.struts.action.LOCALE",PageContext.SESSION_SCOPE) 

should be used instead

pageContext.getAttribute("org.apache.struts.action.LOCALE",PageContext.REQUEST_SCOPE)

Upvotes: 0

Ken Gentle
Ken Gentle

Reputation: 13357

I can't find a constant org.apache.struts.action.LOCALE in the Struts 1.x documentation - should it be org.apache.struts.Globals.LOCALE_KEY? Or one of the other LOCALE_KEY constants?


Edit: org.apache.struts.action.LOCALE is the value of the org.apache.struts.Global.LOCALE_KEY - so the value itself, used as a key, shouldn't be the problem.

Verify that a LOCALE is being set in the Request. My understanding is that the LOCALE_KEY is set in PageContext.SESSION_SCOPE if it is set.

Upvotes: 2

Related Questions