Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is best practice to implement i18n usage java? [closed]

what is best practice to implement i18n using java ?

like image 605
Sun Java its my life Avatar asked Oct 27 '10 15:10

Sun Java its my life


1 Answers

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

like image 176
OscarRyz Avatar answered Sep 22 '22 03:09

OscarRyz