spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.25k stars 37.98k forks source link

Addition of a MessageTool for Velocity views (but not tied to Struts) [SPR-141] #4873

Closed spring-projects-issues closed 20 years ago

spring-projects-issues commented 20 years ago

Michael Ward opened SPR-141 and commented

The ability to utilize i18n message strings (defined in property files) from within Velocity Scripts. The Velocity Tools project already has a MessageTool which allows Velocity Scripts access to the message strings defined in the message resource bundles of the Struts framework.

However the dependency on Struts is limiting, especially when using Velocity with the Spring MVC framework. The MessageTool I wrote for Spring ties the VelocityViewResolver\VelocityView with the MessageSourceAccessor provided by Spring.

This will provide Velocity views with simple i18n support.

example message.properties:

code1=hello code2=foo {0}

example test.vm:

code1: $message.get("code1") code2: $message.get("code2", ["bar"]) code3: $message.get("code3", "default")


Attachments:

spring-projects-issues commented 20 years ago

Michael Ward commented

cvs diff for src/org/springframework/web/servlet/view/velocity/

spring-projects-issues commented 20 years ago

Michael Ward commented

update to JUnit test for new MessageTool. Also removed TestVelocityEngine inner class from test and replaced with MockClassControl implementation.

spring-projects-issues commented 20 years ago

Michael Ward commented

new MessageTool.java class file

spring-projects-issues commented 20 years ago

Juergen Hoeller commented

Michael,

The idea is good, of course - Velocity templates do need easy access to localized messages. However, I'm not sure if we need to mirror the Struts MessageTool here.

Spring already provides the RequestContext class exactly for exposure as model attribute, mainly for template views. If you set VelocityView's "requestContextAttribute" respectively VelocityViewResolver's "requestContextAttribute" to for example "rc", you'll be able to access messages via the RequestContext in your view:

$rc.getMessage("code1")

but also other functionality:

$rc.getErrors("myFormObject") $rc.getContextPath()

While MessageSourceAccessor is a generic helper class, RequestContext specifically targets web views: For example, it supports HTML-escaping messages, either as default setting or specified as flag per message.

So if you miss anything specific, I suggest to add it to the existing RequestContext class. Do you see any special advantage of separately imitating Struts' MessageTool, other than making it easier to convert Velocity templates that have been written for Struts?

Juergen

spring-projects-issues commented 20 years ago

Michael Ward commented

Juergen - Thank you for your prompt response. I was unaware of the RequestContext within the Spring framework. I searched high and low trying to figure out how to obtain Message's from Velocity when using the Spring framework. Apparently I didn't search high enough (or low enough) ;-)

Thanks again, Mike

spring-projects-issues commented 20 years ago

Juergen Hoeller commented

The existing RequestContext and MessageSourceAccessor classes are preferable solutions.