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?
LOCALE is the value of the org. apache.
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.
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.
At the moment I am using this :
<c:set var="localeCode" value="${pageContext.response.locale}" />
This can later be access by using ${localeCode}
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
%>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With