SpringMVC messageTool written by self for velocity I18N

本文介绍了一种在SpringMVC框架中使用Velocity模板引擎实现国际化的方法,通过自定义MessageTool类来支持从属性文件中获取i18n消息字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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]



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值