Message parameters as JS variables

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

Message parameters as JS variables

cooldman224
I have a situation where I'd like to pass JS variables as message parameters for a localized string.

For example, given the message property:
some.message=Hello {0} and {1}!
and in the <script th:inline="javascript">
var zero = 0;
var one = 1;
var msg = [[#{some.message(zero, one)}]];
It is evaluated as:
msg = Hello zero and one!
When it should be:
msg = Hello 0 and 1!

Is there a way to achieve this?
Reply | Threaded
Open this post in threaded view
|

Re: Message parameters as JS variables

Zemi
Administrator
Hello,

are 'zero' and 'one' context attributes?

Please try

  var msg = [[#{some.message(${zero}, ${one})}]];

Regards,
  Zemi

Reply | Threaded
Open this post in threaded view
|

Re: Message parameters as JS variables

cooldman224
Zemi,

So zero and one are not context attributes (which I assume you asking are passed from the Spring @Controller using a Model) but rather JS variables initialized in the <script>.

The variables' values are not passed as parameters to the message but instead their name. I want their values passed.
Reply | Threaded
Open this post in threaded view
|

Re: Message parameters as JS variables

Zemi
Administrator
Hi,

Ok, I understand.

The problem is that Thymeleaf code is executed on the server-side before the HTML is sent to the browser.

So Thymeleaf do not know about the values of zero and one.

You would have to look for some Javascript mechanism to do the substitution.

Regards,
   Zemi

Reply | Threaded
Open this post in threaded view
|

Re: Message parameters as JS variables

cooldman224
Zemi,

Yeah that makes sense. I thought there might be some way with Thymeleaf. Instead, I'll substitute the placeholders (like {0} for example) in the JS. Thanks for the help!