Localized Resource Properties with Java Mustache

One of the hallmarks of Mustache in its purest form is the idea of being "logic free," making it simple and very fast. The Bootique MVC Module uses the Spullara Java Mustache implementation which seems to be very close to the original "logic free" ideal. With that in mind I thought I'd have to roll my own function for displaying Localized Resource Properties, but I happily discovered it is already baked in to the Spullara Mustache implementation. And as we'll see, it is very easy to use as well.

In our example we're going to display the Jumbotron Title and Subtitle of our NixMash Microservices app.

Our title and subtitle are found in resources/messages.properties.

Translate Bundle Function

Here's a test which shows how simple it is to add a Resource Bundle. The String bundle = "messages" is our messages.properties Properties file name and is located in the root of our /Resources directory. ".properties" is added by default. Then we simply add the bundle to our Template Model with TranslateBundleFunction().

Displaying the Property on the Page

To display the property on the page we use the {{#trans}}property{{/trans}} syntax, where the property name is between the tags. Here's our test template.

Source Code Notes for this Post

Source code discussed in this post is found in NixMash Microservices on GitHub. Code for this particular post is found in branch microservices-v0.1.0 – Initial Layout and Structure.