Dynamic Names in Spring Internationalization

I have several properties in the properties file that need to be translated into different languages:

list.var1=XXX
list.var2=XXX
list.var3=XXX

They are list values, so in JSP I want to get the translated value. Therefore, I have a property, for example, myVar, whose values ​​can be {var1, var2, var3}, and I want to get the message "list. $ {MyVar}".

The problem is that in the fmt: message tag, the key attribute does not accept expressions.

<%@ taglib prefix="fmt" uri="java.sun.com/jstl/fmt" %>
<fmt:message key="list.${myVar}"/>

What is the best way to do this?

Thank.

+5
source share
1 answer

It should work the way you want: <fmt:message key="list.${myVar}"/>

Since the lib tag definition contains this key, this expression is: fmt.tdl:

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">

  <description>JSTL 1.1 i18n-capable formatting library</description>
  <display-name>JSTL fmt</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>fmt</short-name>
  <uri>http://java.sun.com/jsp/jstl/fmt</uri>
...
<tag>
    <description>
        Maps key to localized message and performs parametric replacement
    </description>
    <name>message</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.fmt.MessageTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Message key to be looked up.
        </description>
        <name>key</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
...

, can

<%@ taglib prefix='spring' uri='http://www.springframework.org/tags'%>
...
<spring:message code="myPrefix.${transaction.state}"/>
+3

All Articles