views:

68

answers:

4

what is best practice to implement i18n using java ?

+1  A: 

I'd recommend looking at ResourceBundles.

It's a complicated question, because if you have a database you'll want to I18N it as well.

duffymo
+2  A: 

The Java Tutorials have an Internationalization trail. It covers the core language features for Internationalization.

R. Bemrose
A: 

This is basically a matter of the domain. If you are on the web, most major frameworks will provide a way for I18N (in spring, this works with .properties files and taglibs): http://viralpatel.net/blogs/2010/07/spring-3-mvc-internationalization-i18n-localization-tutorial-example.html

For desktop applications on contrary, resource injection might be an interesting option. The Spring Application Framework goes even further and lets you configure named Swing components (buttons, Labels,...) entirely from configuration files. With that you can set the colors and borders of components but also their texts.

matt
A: 

You can use ResourceBundle.getBundle( name ) which returns the correct bundle according to the user locale and get specific messages.

The way the ResouceBundle class works, is, try to load a budnle ( usually a .properties file ) with the localized messages. For instance you can have:

messages_en.properties
-----
greeting = "Hello "

and

messages_es.properties
-----
greeting = "Hola "

and use it as follows.

... void main( ... . {
     ResourceBundle bundle = ResourceBundle.getBundle( "messages", userLocale );
     System.out.println( bundle.getString("greeting" )  + " Steve " );
 }

And it will print

Hello Steve

if user locale is english ( en ) , and

Hola Steve

if user locale is spanish ( es )

The method ResouceBundle.getBundle(), not only loads .properties files, if available it may also load a class which in turn may load message from a database.

See also:

ResourceBundle

Internationalization Quick Intro

OscarRyz