Arthur Portas
Arthur Portas

Reputation: 129

JSTL i18n fmt properties setup

Trying to use JSTL to have an i18n app, I have this:

<li><a href="admin/insertEmployee.jsp"><fmt:message key="new"/></a></li>

But on browser it does not translate the correspondent key 'new' and displays ???new??? instead of value defined in properties file as an HTML anchor (should be 'Novo', in pt_PT).

I have the following files under a package:

Tried to define a default locale inside web.xml (pt_PT), but still not working...

Do I need to define a <fmt:setLocale />?
Is this the correct URI:
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

In web.xml:

<context-param>
    <param-name>
        javax.servlet.jsp.jstl.fmt.localizationContext
    </param-name>
    <param-value>com.arthurportas-i18n.messages</param-value>
</context-param>
<context-param>
    <param-name>
        javax.servlet.jsp.jstl.fmt.fallbackLocale
    </param-name>
    <param-value>pt_PT</param-value>
</context-param>
<context-param>
    <param-name>
        javax.servlet.jsp.jstl.fmt.locale
    </param-name>
    <param-value>pt_PT</param-value>
</context-param>

Upvotes: 3

Views: 6690

Answers (2)

adben
adben

Reputation: 606

@arthur-portas: Only strange behavior is that selecting english translation through clicking on link, requires double click!! A single click is ignored...Is this a browser cache behavior?

That's because you set the locale after the setBundle. the correct order is:

<c:if test="${param['lang'] !=null}">
    <fmt:setLocale value="${param['lang']}" scope="session" />
</c:if>
<fmt:setBundle basename="com.arthurportas.i18n.Messages"/>

Upvotes: 3

Arthur Portas
Arthur Portas

Reputation: 129

Solved: I defined default locale pt_PT in web.xml

<context-param>
    <param-name>
        javax.servlet.jsp.jstl.fmt.locale
    </param-name>
    <param-value>
        pt_PT
    </param-value>
</context-param>

and inside index.jsp, code to declare resource bundle and overriding locale if provided by url as a parameter(example-> /?lang=en_US)

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<fmt:setBundle basename="com.arthurportas.i18n.Messages"/>
<c:if test="${param['lang'] !=null}">
    <fmt:setLocale value="${param['lang']}" scope="session" />
</c:if>

used scope="session", applying to all jps's. The presentation link to change language(english)

<a href="?lang=en_US"><img src="img/UKFlag_32_32.png"></img></a>

and i have two files under package com.arthurportas.i18n: Messages_pt_PT.properties and Messages_en_US. Inside jsp files text translated using for example:

<fmt:message key="employee"/>

Only strange behavior is that selecting english translation through clicking on link, requires double click!! A single click is ignored...Is this a browser cache behavior?

Upvotes: 1

Related Questions