Messages and ResourceBundles

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

Messages and ResourceBundles

hubbardr
Is there a way to use Thymeleaf such that it would resolve a bundle for syntax #{bundleName.key} as a generic java resource bundle instead of assuming the bundle name and location always correspond to the template name and location? In other words, "bundleName" is a legit ResourceBundle on the classpath but it may have a different name and might even been located in a jar on the classpath.

I'd like template "HelloWorld.html" to use bundle at "AppI18nBundle.properties" which is located at the root of the classpath.

We have 1000's of translations that I would prefer to leave where they are vs splitting up for each individual template. Additionally, at the time the Thymeleaf Context is created, it is not known which bundles are required. Messages may be local to the template but may not be.


Possible?
Reply | Threaded
Open this post in threaded view
|

Re: Messages and ResourceBundles

danielfernandez
Administrator
Yes, possible.

Depending on whether you are using Thymeleaf with Spring MVC or not, though, you can choose from two options:

1. If you are using Spring MVC, Thymeleaf will automatically select a strategy for obtaining resource files not based on .properties files with the same name as the template, but on querying Spring's MessageSource objects, which you might have already declared and should already be pointing to whichever messages resource bundles you like. This works then as with any other Spring MVC application.

2. If you are not using Spring MVC, and you do not like the default strategy for obtaining .properties, you can create your own implementation of org.thymeleaf.messageresolver.IMessageResolver --probably by extending AbstractMessageResolver-- implementing whichever method you like for reading messages (for example, from that file you mention). Then simply set it to your TemplateEngine with "templateEngine.setMessageResolver(myMessageResolver)" and you'll be done.

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

Re: Messages and ResourceBundles

hubbardr
No Spring MVC unfortunately. I've attempted creating my own resolver but how do I get to it from the template? It seems as if I need to go through the main template name as specified when you call templateEngine.process ("myTemplate", vars).

Then in the template I would call #{myTemplate.key}.  Can I create a custom messageResolver where the parser will recognize #{myBundleName.key}? At the moment I'm not sure how to tell the parser to recognize my bundle name.

That is where I am stuck.
Reply | Threaded
Open this post in threaded view
|

Re: Messages and ResourceBundles

danielfernandez
Administrator
Once you set your Message Resolver, if you do it with "setMessageResolver" it will substitute the standard one (the one that looks for the .properties files with same name as the templates), so whenever you do #{mykey} from one of your templates it will be your message resolver the one that will be queried for the "mykey" key.

No need to specify the template name or anything. Just use the #{...} syntax as usually, specifying keys from your .properties file.

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

Re: Messages and ResourceBundles

hubbardr
bah! I had a syntax issue that made it seem like the parser didnt like the key. Got it working as described. ty

Reply | Threaded
Open this post in threaded view
|

Re: Messages and ResourceBundles

cikumbrikas
This post was updated on .
Hi,

I have a problem with a messages resolver when using the layout dialect.
I am using spring3+thymeleaf+thymeleaf layout dialect.


Some template file testLayout1.html is using the layout template mainLayout.html.
The begining of the testLayout1.html is:
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="thymeleaf/layout/mainLayout" >
...

The problem is, that the layout file mainLayout.html doesn't use the mainLayout.properties file, but the testLayout1.properties.
In the mainLayout.html there is  a html piece:
< h1 th:text="#{mainTitle}"> Main layout </h1>


When  I write
mainTitle=main layout title
in the mainLayout.properties,
then in the browser output I receive ??mainTitle_lt?? (lt is my locale - Lithuania ).


But when i put
mainTitle=title from child
int the testLayout1.properties
in the browser output I receive: "title from child", OK.


But that is *not good* because the translations of common strings, used mainLayout.html needs to be copied between multiple *.properties file.

Do you know how to solve this problem? How to force thymeleaf using  correct properties files for each template in the layout hierarchy?

p.S.
my Thymeleaf version is 2.1.2.RELEASE and thymeleaf layout version is 1.2.2

Reply | Threaded
Open this post in threaded view
|

Re: Messages and ResourceBundles

Emanuel
Administrator
I'll admit I've never tested using the layout dialect with properties files, but I'm not surprised that there's a mismatch.  The layout dialect works by bringing the layout template into the file that's currently being processed, so as far as Thymeleaf is concerned you're still working on the child template instead of the layout template, and so it'll resolve properties to the child template properties file only.

I'll need to look into this, investigate if it's possible to redirect the message resolver, or to add another lookup path to the message resolver, when working with properties files.

GitHub issue: https://github.com/ultraq/thymeleaf-layout-dialect/issues/36