Passing a fragment to a message expression

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

Passing a fragment to a message expression

thomas.heigl
Hi,

Is it possible to pass an evaluated fragment to a message expression?

Given this fragment:

<div th:fragment="link(url, text)" th:remove="tag">
   <a th:href="@{${url}}"><span>${text}</span></a>
</div>

And a message expression:

home.welcome=Hello User! See new content at {0}. 

I want to pass the evaluated fragment to the message expression (pseudo-code):

<p th:utext="#{home.welcome(${link:: link(url='myUrl', text='myText')})}"></p>

To get resulting HTML like this:

<p>
    Hello User! See new content at <a href="myUrl"><span>myText</span></a>.
</p>

I'm used to this approach from templates in Apache Wicket and I'm wondering how to do the same thing in Thymeleaf without putting the HTML into the message property itself.

I have posted a question with a bounty on StackOverflow in case anyone wants to answer there:

https://stackoverflow.com/questions/47023365/how-to-pass-a-fragment-to-a-message-expression-in-thymeleaf

Thomas
Reply | Threaded
Open this post in threaded view
|

Re: Passing a fragment to a message expression

Metroids
I saw the question on stack overflow... I can't think of a way this is possible.
Reply | Threaded
Open this post in threaded view
|

Re: Passing a fragment to a message expression

thomas.heigl
You might be right, but how do you do non-trivial i18n in Thymeleaf then?

How do you internationalize a text with links or images? Is it really necessary to put the markup into the message itself and duplicate it there? I can't imaginge that this issue never came up before.
Reply | Threaded
Open this post in threaded view
|

Re: Passing a fragment to a message expression

Metroids
I guess my question is, does the html need to be different between different localizations?  Why do you just pass in the localized strings in the fragment itself.

If you have

<div th:fragment="link(url, text)" th:remove="tag">
   <a th:href="@{${url}}"><span>${text}</span></a>
</div>

<div th:replace="link :: link (url=#{home.welcome.url}, text=#{home.welcome.text})">...</div>
Reply | Threaded
Open this post in threaded view
|

Re: Passing a fragment to a message expression

thomas.heigl
I don't only want to localize the link text, but the whole sentence the link is part of.

A solution is to split the localizations, but then I end up with multiple properties per sentence: E.g.

home.welcome=Hello User! See new content at {0}. More info.

becomes:

home.welcome.part1=Hello User! See new content at 
home.welcome.part2=. More info.

And in the template I'd use:

<span th:text="#{home.welcome.part1} />
<div th:replace="link :: link (url=#{home.welcome.url}, text=#{home.welcome.text})">...</div>
<span th:text="#{home.welcome.part2} />

This works for one language, but might not work for languages where the word order or sentence structure is different.

The only real solution would to "inject" fragments into message expressions.
Reply | Threaded
Open this post in threaded view
|

Re: Passing a fragment to a message expression

yvanavermaet
It's not possible afaik.

Only thing you could do, is add the html markup to the i18n files...

home.welcome=Hello User! See new content at <a href="{0}">${1}. More info.

I'm not a big fan of adding HTML to i18n, but for this use case (and the requirement of having multiple languages), I don't see any other way at this time.