开发者

Is it possible to read static text dynamically from property files in velocity template?

greetings all i have a java ee application (spring framework) that uses vm templates that contains static texts like:

<span> hello world </span>

to be something like:

<span> <fmt:message key="hi.message" /> </span>

and i was wondering if it's possible to read that texts from a property file(en/fr) depending on the user locale like in JSP, so that i will use one template for all locales and the text is dynamic

Note: velocity is not my view technology used in the 开发者_运维技巧app, i am using it's templates in sending emails only.


Velocity Tools' ResourceTool class is a tool for accessing ResourceBundles and formatting messages therein. An answer to a previous question describes how to set up Velocity Tools

In the tools configuration file, add the following line to enable ResourceTool. You can provide a default locale, but normally the locale from HttpServletRequest.getLocale() will be used.

Toolbox configuration example:
 <tools>
   <toolbox scope="request">
     <tool class="org.apache.velocity.tools.generic.ResourceTool"
              bundles="myresources"
              locale="en_US"/>
   </toolbox>
 </tools>

If your resource bundle contains

bar=The args are {0} and {1}.

you can use the following in your template

$text.bar                 ->  The args are {0} and {1}.
$text.bar.insert(4)       ->  The args are 4 and {1}.
$text.bar.insert(4,true)  ->  The args are 4 and true.

Maybe this is best shown using a fully programmatic configuration; this way you can manually set the locale each time.

EasyFactoryConfiguration config = new EasyFactoryConfiguration();
config.toolbox("request").tool(ResourceTool.class)
    .property("bundles", "myresources")
    .property("locale", "en_US");

ToolManager manager = new ToolManager(false, false);
manager.configure(config);

Context context = manager.createContext();
context.put("name", "Jarl");

Template template = Velocity.getTemplate("mytemplate.vm");

StringWriter writer = new StringWriter();
template.merge(context, writer);
System.out.println(writer.toString());

mytemplate.vm:

$text.greeting $name

myresources_en_US.properties:

greeting=Hello

Output

Hello Jarl


Spring MVC comes with (very) useful velocimacros (see Spring MVC documentation). One of them is #springMessageText.

In your hello.vm file:

<span>#springMessageText("hi.message", "Hello Default!")</span>

This macro will read the message from your message sources, depending on the current locale (using the built-in ResourceBundleMessageSource from Spring).

messages_fr_FR.properties

hi.message=Bonjour

messages_en_GB.propertie

hi.message=Hello

If no bundle is available for the current locale, the default message "Hello Default!" is used.

By default, Spring is reading messages*.properties files. But you can specify more message sources in your servlet.xml configuration (here, messages*.properties and othermessages*.properties):

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
      <list>
        <value>messages</value>
        <value>othermessages</value>
      </list>
    </property>
</bean>

How does SpringMVC knows about the current locale?

Well, it's all built-in SpringMVC. The ResourceBundleMessageSource reads keys according to the Locale. I think a LocaleResolver is configured by default (using the locale sent along the client request), but you can register your own localeResolver.

I encourage you to check all the available springmvc velocimacros and velocity tools (very useful!).


this is because of your encoding, try at first to manually change the page encoding (usually in tools).

i got a bit farther in this but not much farther..Spring automatically sets the encoding to iso-8859-1, i made it set it to utf-8, however, my hebrew strings changed to gibrish, also destroyed the chance to hand-pick the right encoding as none of em seem to decode it right.

i'd really appreciate a hand in solving this problem.


We cannot use macros outside the web app, like in my case so a solution would be to use messageSource

model.put("messagesource", this.messagesource);
// adding this e.g. should do the trick
model.put("keyHelloCustomer", "the_key_you_use_in_your_properties_file");
model.put("noArgs", new Object[]{});
model.put("locale", yourOwnPrivateMethodToDetermineWhichLocaleToUse()); 

and in the vm:

${messageSource.getMessage($keyHelloCustomer, $noArgs, $locale)}

reference: http://forum.springsource.org/showthread.php?t=82018

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜