Spring国际化中的动态名称 [英] Dynamic names in Spring internationalization

查看:112
本文介绍了Spring国际化中的动态名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在属性文件中有几个要翻译成不同语言的属性:

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

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

它们是列表的值所以在JSP中我想获得翻译的值。所以我有一个名为myVar的属性,其值可以是{var1,var2,var3},我想得到消息list。$ {myVar}。

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 want to get the message "list.${myVar}".

问题是在fmt:message标签中,key属性不接受表达式。

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}"/>

最好的方法是什么?

谢谢。

推荐答案

它应该按您想要的方式工作:< fmt:message key =list。$ {myVar}/>

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

因为标签lib定义允许该键是一个表达式:
fmt.tdl:

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>
...

在应用程序中失败的是

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

这篇关于Spring国际化中的动态名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆