15

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?

Fischermaen
  • 12,238
  • 2
  • 39
  • 56
Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179

10 Answers10

20

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>
Community
  • 1
  • 1
dawez
  • 2,714
  • 2
  • 29
  • 50
  • Agreed with @BalusC - one of the last two solutions should be the way to go. Don't go scriptlets... ever... – David Welch Dec 03 '12 at 18:30
  • Do you happen to know why `pageContext.response.locale` would return null when the client is forwarded to a custom error page (403) following a `PUT` request?...For all other cases, this works ok. – theyuv Sep 22 '17 at 14:59
6

in Struts2 try

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

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
  • 12,458
  • 4
  • 40
  • 51
2

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
  • 13,277
  • 2
  • 41
  • 49
1

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

PbxMan
  • 7,525
  • 1
  • 36
  • 40
1

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>
Thilina Rubasingha
  • 1,049
  • 1
  • 9
  • 17
1

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
  • 11,608
  • 5
  • 46
  • 60
0

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
  • 76,835
  • 68
  • 152
  • 179
0
<%@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>
<%}%>
Anton Moiseev
  • 2,834
  • 4
  • 24
  • 30
0

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}

fuxia
  • 62,923
  • 6
  • 54
  • 62
jeremy
  • 1