Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for translations

Tags:

java

jsp

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 formatfunction (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.

like image 740
Tom Marthenal Avatar asked Dec 28 '22 01:12

Tom Marthenal


1 Answers

Don't reinvent. Use JSTL fmt taglib. It supports parameterized messages as well.

<fmt:message key="identifier">
  <fmt:param value="${username}" />
</fmt:message>

See also:

  • How to internationalize a Java web application? - a mini tutorial
like image 104
BalusC Avatar answered Jan 11 '23 02:01

BalusC