How do I access Locale from a JSP?
Asked Answered
V

10

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?

Vereeniging answered 2/12, 2008 at 11:50 Comment(0)
B
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>
Berm answered 10/11, 2009 at 18:34 Comment(2)
Agreed with @BalusC - one of the last two solutions should be the way to go. Don't go scriptlets... ever...Expensive
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.Sealer
W
6

in Struts2 try

<s:if test="#request.locale.language=='us'">
     <s:select name="gender" list="#{'M':'Male','F':'female'}" ></s:select>
 </s:if>
W answered 26/7, 2011 at 13:29 Comment(0)
M
3

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

Locale locale = (locale)request.getSession().getAttribute(Globals.LOCALE_KEY);
Marou answered 22/3, 2009 at 6:10 Comment(0)
B
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.

Blackness answered 2/12, 2008 at 13:30 Comment(0)
L
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"]}'/>
Lenis answered 7/4, 2010 at 17:16 Comment(0)
C
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

Chirlin answered 25/3, 2013 at 4:27 Comment(0)
C
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>
Casals answered 23/4, 2014 at 10:45 Comment(0)
V
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)
Vereeniging answered 2/12, 2008 at 14:5 Comment(0)
B
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>
<%}%>
Booma answered 9/12, 2008 at 12:56 Comment(0)
C
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}

Comitative answered 14/11, 2011 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.