I'm writing a website using JSP. I want to have the website available in multiple languages, so I've created a HashMap
for each language I plan to support, and am finding the text via map.get("identifier")
(with some other code, of course.)
The problem I'm having is one I've solved before by using a format
function (similar to printf
in many languages), but this was in another language.
The problem specifically is that text like User performed action
may be come Action was performed by user
in another language (i.e. the terms may become out of order).
In the past, I've done something like #translate("Welcome to the site, %s!", {"Username"})
, and then used the language's format
function to replace %s
with the username. I could simply use String#replace
but then I can't do something like #translate("Welcome to the site, %s! You last visited on %s!", {"username", "last visit"})
like I'd like to.
Sorry if this is a bad explanation—just look up printf
in something like PHP.
What would be the best way to replicate something like this in Java? Thanks for the help.
Don't reinvent. Use JSTL fmt
taglib. It supports parameterized messages as well.
<fmt:message key="identifier">
<fmt:param value="${username}" />
</fmt:message>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With