Thymeleaf: Can I use messages inside expressions
Asked Answered
A

1

5

I am using Thymeleaf 3 within a Spring Boot application. Currently I am in a situation where I want to use a message expression inside an EL expression (Spring EL).

First use case: trim the message

data:title="${#{message.key}.trim()}

Second use case: conditionally create an attribute with a message as its value

data:title="${condition ? #{message.key} : ''}

Both examples will produce a syntax error, because #{ is not an allowed start of an expression.

Any ideas how to achieve what I want?

Alee answered 12/1, 2018 at 17:16 Comment(2)
Checkout the following tutorial: thymeleaf.org/doc/tutorials/2.1/…Bucksaw
@Bucksaw I did. Unfortunately chapter "4.1 Messages" doesn't say anything about this. :( Apparently I had to read the entire tutorial ...Alee
V
15

In both cases you'll want to use the #messages utility object.

data:title="${#messages.msg('key').trim()}"

data:title="${condition ? #messages.msg('key') : ''}"
Vulcanism answered 12/1, 2018 at 17:25 Comment(1)
Ah, I missed that. I already was on the verge to create my own Spring bean that would exactly do what #messages is doing. Thanks a lot!Alee

© 2022 - 2024 — McMap. All rights reserved.