3

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.

Peter Samyn
  • 156
  • 3
  • 14

2 Answers2

3

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;
}
riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
  • Thank you for the response. I believe you have gotten me nearly there but I get this error: The value of attribute "th:with" associated with an element type "null" must not contain the '<' character. Is there a way to fix this while still using unescaped text? – Peter Samyn Nov 02 '16 at 19:33
  • are one of your values that you are supplying null? I'd look at `click.here` – riddle_me_this Nov 02 '16 at 19:34
  • click.here is not null as far as I can tell. I can use it without the parameters just fine. I thought maybe the '<' needed to be escaped in some manner but am not sure. – Peter Samyn Nov 02 '16 at 19:39
  • Found that removing '<' allows it to function. Just need the escaped values for '<' and '>'. '<' and '>' respectively. – Peter Samyn Nov 02 '16 at 19:53
  • Ok, the other thing to note is that in my config for the resolver, I use TemplateMode.HTML with a CharacterEncoding of UTF-8. – riddle_me_this Nov 02 '16 at 19:57
  • I'm on Thymeleaf 2.1.5 with an xml config due to the legacy system I am working with. I believe I have the analogous configurations. – Peter Samyn Nov 02 '16 at 20:28
  • Thinking that the older version would explain that discrepancy – riddle_me_this Nov 02 '16 at 20:31
  • A more reasonable escape sequence can also be found here. http://stackoverflow.com/questions/16360009/error-parsing-thymeleaf-template-with-1-0 – riddle_me_this Nov 02 '16 at 21:14
0

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>
Martin Frey
  • 10,025
  • 4
  • 25
  • 30