keyword:SpringMVC velocity I18N MessageTool
Addition of a MessageTool for Velocity views (but not tied to Struts)
Description
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.
so we written this manul MessageTool, like these codes:package org.springframework.web.servlet.view.velocity;
import org.springframework.context.support.MessageSourceAccessor;
import java.util.List;
import java.util.Locale;
/**
* Similar to the MessageTool defined in the Velocity Tools project, however this is not tied to Struts.
*/
public class MessageTool {
private MessageSourceAccessor messageSourceAccessor;
private Locale locale;
public MessageTool(MessageSourceAccessor messageSourceAccessor, Locale locale) {
this.messageSourceAccessor = messageSourceAccessor;
this.locale = locale;
}
public Locale getLocale() {
return this.locale;
}
public String get(String code) {
return messageSourceAccessor.getMessage(code, locale);
}
public String get(String code, String s1) {
return messageSourceAccessor.getMessage(code, s1, locale);
}
public String get(String code, Object[] args) {
return messageSourceAccessor.getMessage(code, args, locale);
}
public String get(String code, Object[] args, String defaultMsg) {
return messageSourceAccessor.getMessage(code, args, defaultMsg, locale);
}
public String get(String code, List args) {
return this.get(code, args.toArray());
}
public String get(String s, String s1, List list) {
return this.get(s, list.toArray(), s1);
}
}
messageResource.xml:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>/WEB-INF/messageResource/MessageResource</value>
</list></property>
TEST CASE:
private static MessageSource messageSource;
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
messageSource = (MessageSource) applicationContext.getBean("messageSource");
MessageTool messageTool = new MessageTool(new MessageSourceAccessor(messageSource), locale);
ToolContext context = manager.createContext();
context.put("messageTool", messageTool);
System.out.println("-------begin---------");
System.out.println(messageTool.get("code1"));// hello
System.out.println("-------end-----------");
example message_en_US.properties:
code1=hello
code2=foo {0}
example test.vm:
code1: $message.get("code1")
code2: $message.get("code2", ["bar"])
code3: $message.get("code3", "default")
lean more about plse take step into this link[https://jira.springsource.org/browse/SPR-141?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel#issue-tabs]