tags:

views:

3193

answers:

6

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 can i solve this?

+1  A: 

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.

Ken Gentle
A: 

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)
Sergio del Amo
A: 

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

<%Locale locale = (Locale)session.getAttribute(Globals.LOCALE_KEY); if(locale.getLanguage().equals("fr")){%>

/lib/js/jscalendar-1.0/lang/calendar-fr.js">

<%}else {%>

/lib/js/jscalendar-1.0/lang/calendar-en.js">

<%}%>

A: 

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

Locale locale = (locale)request.getSession().getAttribute(Globals.LOCALE_KEY);
Faisal Feroz
+2  A: 

HI

at the moment I am using this :

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

This can later be access by using ${localeCode} or inside a scriptlet by querying

<%
  Object ob_localeCode = pageContext.getAttribute("localeCode");
  if (ob_localeCode != null) {
    String currentLanguageCode = (String) ob_localeCode;
  }
  //more code
%>

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>
dawez
+1 for a scriptletless way.
BalusC
A: 

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"]}'/>
Carles Barrobés