Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Internationalization

 
James Edkins
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the java.util.ResourceBundle class.
David
Sun Certified Programmer for the Java2 Platform
 
Phil Hanna
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
key=value
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
 
James Edkins
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I will look into that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic