i18n with FileTemplateResolver

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

i18n with FileTemplateResolver

jfisbein
Hi,
   I'm loading templates from an external folder using an FileTemplateResolver
   I'm having problems to make i18n work.
   I don't know how to tell to thymeleaf to automatically get the properties files for i18n from the same folder than the template.
   Can someone help me?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: i18n with FileTemplateResolver

Emanuel
Administrator
That feature should work without having to do any additional configuration - Thymeleaf's standard message resolver will use the same resource resolution mechanism as the template resolver you've chosen, and then look for properties files in the same folder as the template, with the same name as the template.

Just to try it out, I set up a project that had a template with a few external text expressions in it, About.html, and configured to get it using a FileTemplateResolver.  Then I put a properties files into the same folder, About_en.properties, which had all the property keys I was after, but made no additional configurations to Thymeleaf.  When Thymeleaf processed the template, it found my properties file and filled-in all the proper texts.
Reply | Threaded
Open this post in threaded view
|

Re: i18n with FileTemplateResolver

jfisbein
I'm using Spring and thymeleaf-spring3.
I finally solved adding a ReloadableResourceBundleMessageSource to my Spring context:

        <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="file:/var/tmp/thymeleaf_resources/messages" />
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="cacheSeconds" value="60" />
        </bean>

and now Thymeleaf is looking for messages_xx.properties files in that folder.

I really would like to understand why it wasnt working the other way.

thanks.
Reply | Threaded
Open this post in threaded view
|

Re: i18n with FileTemplateResolver

danielfernandez
Administrator
When Thymeleaf is used in a Spring MVC application, it is automatically set to delegate all Message resolution to Spring's MessageSource implementation.

This is done so in order to correctly integrate Thymeleaf's text externalization features with Spring's. The only correct way to customize message resolution in a Spring application (with thymeleaf or not) is creating a MessageSource implementation like you've done.

If Thymeleaf and Spring were using different different message resolution mechanisms, many parts of Spring integration (like for example error messages in forms) would be seriously mangled.

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: i18n with FileTemplateResolver

jfisbein
Ok, now it make sense.
Thanks a lot for your response! :-)