开发者

Dynamic names in Spring internationalization

I have in the properties file several properties to be translated into different laguages:

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

They are values of a list so in the JSP I want to get the translated value. So I have a property for example called myVar whose values can be {var1, var2, var3} and I wa开发者_如何学Pythonnt to get the message "list.${myVar}".

The problem is that in fmt:message tag, key attribute doesn't accept expressions.

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

What is the best way to do this?

Thanks.


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

Because the tag lib definition alowes that key is an expression: 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>
...

And what defently worked in may application is

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜