Peter Samyn
Peter Samyn

Reputation: 156

Thymeleaf: Message as a parameter to another Message

I'm new to Thymeleaf and am moving some facelets pages to use Thymeleaf. Due to the legacy system I am on we are using Thymeleaf 2.1.5 with an xml config.

I have run into an issue that I have struggled to find adequate documentation for. What I am trying to accomplish is taking localized messages as parameters in other messages.

One example that I have in several templates is something like this

Some text string <a href="url">CLICK HERE</a>

Where the properties file is like this:

some.text=Some text string {0} 
click.here=CLICK HERE 

I tried doing something like this:

<p th:utext="#{some.text('<a th:utext="#{click.here}" href="url">')}"></p>

but had no luck getting it to work.

Also, is there a way to localize a string and without it being part of its own dom element? For example, I want to place a single string like this:

String 

Instead of this:

<div> String </div>

Any insights would be much appreciated. Thank you.

Upvotes: 3

Views: 3595

Answers (2)

riddle_me_this
riddle_me_this

Reputation: 9145

This works with Thymeleaf 3 for your specific example:

<p th:with="openTag='<a href=\'stackoverflow.com\'>',anchorLabel=#{click.here},closeTag='</a>'" th:remove="tag" th:utext="#{some.text(${openTag+anchorLabel+closeTag})}"></p>

Unfortunately, producing double quotes around the link in the output doesn't seem to work. Don't know whether Thymeleaf supports this. I'll update if I find a way. Single quotes seem to work though.

But in general, you're likely looking for some combination of th:with to create variables. As far as your second question, you will want to use th:remove="tag" to remove tags.

Answer assumes a config like:

@Bean
public ThymeleafViewResolver viewResolver() {

    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setOrder(1);
    resolver.setCharacterEncoding("UTF-8");
    resolver.setTemplateEngine(templateEngine());
    return resolver;
}

@Bean
public ITemplateResolver webTemplateResolver() {

    SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
    resolver.setPrefix("/WEB-INF/thymeleaf/");
    resolver.setTemplateMode(TemplateMode.HTML);
    resolver.setCharacterEncoding("UTF-8");
    resolver.setSuffix(".html");
    resolver.setCacheable(false);
    resolver.setOrder(2);
    return resolver;
}

Upvotes: 3

Martin Frey
Martin Frey

Reputation: 10075

You should really split the text generation with i18n and the link. You can wrap the 'some.text' in a span and render the link after that. Like this you avoid utext and can properly generate the link with a translated text also if that's desired.

One way to avoid having the span in the resulting html would be to use the attribute th:remove="tag".

Another solution should now be possible by using a <th:block>...</> element, if I'm not mistaken, so you don't need to remove a tag after again.

<span th:remove="tag" th:text="#{some.text}">some text that will be replaced but shows in the mock</span> <a href="url" th:text="#{click.me}">CLICK HERE</a>

Upvotes: 0

Related Questions