[ACCEPTED]-How do I access Locale from a JSP?-locale

Accepted answer
Score: 20

At the moment I am using this :

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

This can 7 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 6 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 5 spring 2.5 config at the moment.

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

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

or if you 2 really want to use some short code to impress 1 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" />

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

in Struts2 try

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


Score: 3

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

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

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

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

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

Score: 1

In Struts2, using EL I successfully used:


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

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

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

To 10 get locale from jsp:


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

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

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

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

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

Score: 1

Try with this

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


Score: 0

Ken G. pointed to the answer.


should be used 1 instead

Score: 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>


Score: 0

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

When locale has been changed 1 with this method.

It is not the same as:

More Related questions