spring - Is it possible to read static text dynamically from property files in velocity template? -
greetings have java ee application (spring framework) uses vm templates contains static texts like:
<span> hello world </span>
to like:
<span> <fmt:message key="hi.message" /> </span>
and wondering if it's possible read texts property file(en/fr) depending on user locale in jsp, use 1 template locales , text dynamic
note: velocity not view technology used in app, using it's templates in sending emails only.
spring mvc comes (very) useful velocimacros (see spring mvc documentation). 1 of them #springmessagetext.
in hello.vm file:
<span>#springmessagetext("hi.message", "hello default!")</span>
this macro read message message sources, depending on current locale (using built-in resourcebundlemessagesource spring).
messages_fr_fr.properties
hi.message=bonjour
messages_en_gb.propertie
hi.message=hello
if no bundle available current locale, default message "hello default!" used.
by default, spring reading messages*.properties files. can specify more message sources in servlet.xml configuration (here, messages*.properties , 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 springmvc knows current locale?
well, it's built-in springmvc. resourcebundlemessagesource reads keys according locale. think localeresolver configured default (using locale sent along client request), can register own localeresolver.
i encourage check available springmvc velocimacros , velocity tools (very useful!).
Comments
Post a Comment