how can i use the Locale object to display the string in differnt language
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sumit_Murgai
Posted On:   Tuesday, January 29, 2002 03:16 AM

how can i use the Locale object to display the string in differnt language

Re: how can i use the Locale object to display the string in differnt language

Posted By:   Anonymous  
Posted On:   Tuesday, January 29, 2002 06:43 AM

Take a look at this FAQ: How do I use a ResourceBundle?


Regards

Re: how can i use the Locale object to display the string in differnt language

Posted By:   Christopher_Schultz  
Posted On:   Tuesday, January 29, 2002 06:02 AM

Well, ultimately, you need to do the translation yourself.



Generally, the process is to create a set of properties files. Lets say that you want to have a menu of options:




  1. Create a new file
  2. Edit an existing file
  3. Delete an existing file
  4. Quit



In order to do this, you would first create a default properties file. Typically, this will be written in the language that you speak, or the language of the largest target audience. I'll use English, here.



menu.properties




welcome=Welcome to my menu!
create=Create a new file
edit=Edit an existing file
delete=Delete an existing file
quit=Quit



Now, you write your code like this:




ResourceBundle resources = ResourceBundle.getBundle("menu");

Sysem.out.println(resources.getString("welcome"));
System.out.println();
System.out.print("1. ");
System.out.println(resources.getString("create"));
System.out.print("2. ");
System.out.println(resources.getString("edit"));
System.out.print("3. ");
System.out.println(resources.getString("delete"));
System.out.print("4. ");
System.out.println(resources.getString("quit"));



That's all there is to it. If you want to get crazy and have variables in your messages, that's easy, too:



(in the properties file)


confirm.delete=Are you sure you want to delete {0}?


(in the code)

String filename = ...;
Object[] arguments = new Object[] { filename };

System.out.println(new MessageFormat(resources.getString("confirm.delete")).format(arguments));



Now, when you are ready to translate, you only need to create more properties files.



If you wanted to create a properties file for French speakers, you would create a new file, menu_fr.properties, which would contain the same keys as before (like confirm.delete and welcome), except that they values would be in French, not english.



The ResourceBundle class performs some magic to load the correct properties file for you, so your translation effort only has to include adding properties files.



You should also read the FAQs here in jGuru and on other sites, and especially read the JavaDoc documentation for the Java API. Specifically, read the documentation for the java.util.ResourceBundle class as well as the java.text.MessageFormat class.



Good luck,

-chris
About | Sitemap | Contact