Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access Locale from a JSP?

Tags:

jsp

locale

struts

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?

like image 495
Sergio del Amo Avatar asked Dec 02 '08 11:12

Sergio del Amo


People also ask

What is locale in JSP?

LOCALE is the value of the org. apache.

What is Internationalization in JSP?

Jakarta EE/Java JEE 8 Web Development(Servlet, JSP and JDBC) Internationalization (i18n) − This means enabling a website to provide different versions of content translated into the visitor's language or nationality.

How can we use resource bundle properties file in JSP?

The <fmt:bundle> tag will make the specified bundle available to all <fmt:message> tags that occur between the bounding <fmt:bundle> and </fmt:bundle> tags. With this, you need not specify the resource bundle for each of your <fmt:message> tags.


1 Answers

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>
like image 117
dawez Avatar answered Sep 22 '22 06:09

dawez