문제

We're looking to externalize our JSP's, which currently contain things like this:

<c:when test="${productObject.was}"><span>Was ${price} - now ${salePrice}</span><br /></c:when>

We already have a Java function (lm.getString) that we might be able to use for this purpose - it accepts the English string (including parameter-tags), a hashmap of replacement variables and a language, e.g.

wasPriceString = "Was <price> - now <salePrice>";
lang = "ESP";
HashMap hm = new HashMap();
hm.put("salePrice", price);
hm.put("price", regPrice);
wasPriceString = lm.getString(wasPriceString, hm, lang);

and returns the translated string, with parameters inserted, e.g. "Was $17.99 - now $11.50" (English) or "era $17.99 - ahora $11.50" (Spanish).

We use this in our Java code, but now I'm looking to change the JSP's also. My question is, can this be done easily, in the JSP itself (passing parameters)? If not, what's the best alternative? I'd rather not simply translate the output text, since that's just hacky, and leads to this sort of evil:

<c:when test="${productObject.was}"><span><fmt:message key="textWas_ui"/> ${price} - <fmt:message key="textNow_lc"/> ${salePrice}</span><br /></c:when>

Am I missing something obvious?

도움이 되었습니까?

해결책

Yes, you're missing the fact that <fmt:message> accepts parameters, and uses the standard MessageFormat class behind the scenes. So you just need

<fmt:message key="wasVsNow">
    <fmt:param value="${price}"/>
    <fmt:param value="${salePrice}"/>
</fmt:message>

in your JSP, and, in your properties files containing the internationalized messages:

wasVsNow=Was {0} - now {1}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top