File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Internationalization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Internationalization" Watch "Internationalization" New topic


James Edkins

Joined: Apr 24, 2001
Posts: 2
Does anybody know the best way to have multiple languages on a site, using JSP's, without having to have multiple JSP's for each language?
David Freels
Ranch Hand

Joined: Feb 01, 2001
Posts: 102
Use the java.util.ResourceBundle class.
Sun Certified Programmer for the Java2 Platform
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
Just to elaborate a bit, you can put all the text you wish to localize into a properties file, where each line has the format
where value is the string to be displayed and key is a unique string that you'll use in your program to refer to the entry. Then have that properties file translated (just the values, not the keys - you need to keep the keys the same so that you can refer to them in your program) into each language you wish to support. The base file names of all the properties files must be the same except for a suffix describing the locale. See java.util.ResourceBundle for details.
For example, in a JSP that needs to count from 1 to 3, you need a properties file like this:

and one in French, say

The tricky part is where to put the files. They need to be treated like .class files from the standpoint of the JSP container's class loader. So, for example, you could put them in the web application's /WEB-INF/classes file or a subdirectory from there, if you want to use a package structure:

In the example above, I'm using a subdirectory named "sample".
Then in your JSP:

The resource bundle structure works well in that it automatically allows you to distinguish between Swiss French and Canadian French, for example, and that it looks through the hierarchy of resources for default values.
Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets

Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
James Edkins

Joined: Apr 24, 2001
Posts: 2
Thank you, I will look into that.
I agree. Here's the link:
subject: Internationalization
It's not a secret anymore!