what is best practice to implement i18n using java ?
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 bundle ( 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With