I would like to implement a website, based on JSP and servlet that allows you to change the language from Italian to English without changing page ... how can I change strings of the labels of the html page?
-
http://stackoverflow.com/questions/6448451/java-web-application-i18n – Sunil Singh Bora Feb 26 '14 at 22:08
-
possible duplicate of [How to internationalize a Java web application?](http://stackoverflow.com/questions/4276061/how-to-internationalize-a-java-web-application) – Luiggi Mendoza Feb 26 '14 at 22:23
2 Answers
You could create two different "properties" files, one for each language, and use the same keys in both of them for the different labels in your application.
Then you could name one "messages-en.properties" and the other "messages-it.properties" and in the code you choose which one you wanna use. You can do it dynamically by changing it from your webpage somehow, or just hardcoding it for testing.
An example of the content of the "properties" files would be:
English:
# Comments
page.title = Title of my page
page.subtitle = Subtitle of my page
Italian:
# Commenti
page.title = Titolo della mia pagina
page.subtitle = Sottotitolo della mia pagina
Just grab the message using the key, then depending on which file you chose you'll get the message you want to display. Hope this helps.

- 41
- 4
I suggest you to use the JSTL Internationalization Tag Library, so you can use the same properties files ResourceBundles with the <fmt:message>
tag, here a basic example, I put the code for the link, the bundle is set with the <fmt:setBundle>
tag the basename is the name of your properties files and you access to the values through the key value in the <fmt:message>
, you can se the locale with the <fmt:setLocale>
in this example the value is "en" but you can set it based in the request information.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<title>JSTL fmt:message Tag</title>
</head>
<body>
<fmt:setLocale value="en"/>
<fmt:setBundle basename="com.tutorialspoint.Example" var="lang"/>
<fmt:message key="count.one" bundle="${lang}"/><br/>
<fmt:message key="count.two" bundle="${lang}"/><br/>
<fmt:message key="count.three" bundle="${lang}"/><br/>
</body>
</html>
You can see the JEE Tutorial to get more info: Tutorial

- 2,740
- 4
- 25
- 33