2

Is it possible to use a JS variable inside @Messages as a parameter? When I use this, I get the following error:

not found: value name

var name = "some name";
var title = "@Messages("message", name)"

3 Answers 3

4

It's not possible, @Messages compiled on the server and javascript on the client. It will compile before javascript even if you will pre-compile it with some server side js engine.

Here is special module to compute messages on client side, but I am not sure it is still relevant for Play 2.5:

https://github.com/julienrf/play-jsmessages

Sign up to request clarification or add additional context in comments.

Comments

0

If you are trying to use server data or variables in your JS code, you could use this approach. In some situations, I need my JS to have a list of server items, so in my whatever.scala.html, I create a script tag and initialize a JS variable from the server like below.

<script type="text/javascript">
  // get employee data from our server model - it needs to be converted to JSON for JS
  var employeeJsonString = @{Html(new Gson().toJson(model.getEmployeesAsJson()))};
  var employees = JSON.parse(exmployeeJsonString);
  // then you may use employees in JS code...
</script>

Comments

0

jsMessages is a good solution.

If you want a more generic solution for any kind of code, not just messages, you can try out this: JavascriptRouting

It enables you to pass javascript variables to any call to an endpoint in your application. But true, you would have to set up a Play route for your Messages.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.