Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle multiple languages in Java apps?

I am writing a program use JSP and Java. How can I use property files to support multiple languages?

And by the way, there are always some things like \u4345.
What is this? How do they come?

like image 996
MemoryLeak Avatar asked Aug 13 '09 04:08

MemoryLeak


3 Answers

For the multiple languages, check out the ResourceBundle class.

About the \u4345, this is one of the dark and very annoying legacy corners of Java. The property files need to be in ASCII, so that all non-ASCII characters need to encoded as \uxxxx (their Unicode value). You can convert a file to use this encoding with the native2ascii command line tool. If you are using an IDE or a build tool, there should be an option to invoke this automatically.

If the property file is something you have full control over yourself, you can starting from Java6 also use UTF-8 (or any other character set) directly in the property file, and specify that encoding when you load it:

// new in Java6
props.load(new InputStreamReader(new FileInputStream(file), 'UTF-8'));

Again, this only works if you load the Properties yourself, not if someone else does it, such as a ResourceBundle (used for internationalization).

like image 111
Thilo Avatar answered Oct 09 '22 05:10

Thilo


there is an entire tutorial on http://java.sun.com/docs/books/tutorial/i18n/index.html

This specifies and explains about anything you need to know.

like image 38
Peter Avatar answered Oct 09 '22 05:10

Peter


The Java tutorial on i18n has been mentioned already by Peter. If you are building JSPs you probably want to look at the JSTL which basically allows you to use the functionality of ResourceBundle through JSP tags.

like image 1
Robert Petermeier Avatar answered Oct 09 '22 06:10

Robert Petermeier