• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding ListResourceBundle  RSS feed

 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
If i am using a ResourceBUndle or a ListResourceBUndle class and want to get values from a property file or a class which has a dynamic message i.e {0},{1}, how can i fill the values to them.

For eg In the Properties file i have

error.userName {0} should have atleast {1} character.

In the java class i have

ResourceBundle bundle = ResourceBUndle.getBundle("MyProperties");
String errorMessage = bundle.getString("error.userName");

How to fill th parameters of {0} and {1}.

Thanks and regards,
Pradeep Kumar Bhatt
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the class you are looking for is java.text.MessageFormat.
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i triend understaning the MessageFormat class but could not get it right. Can you help me out with an example.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you try? Can you post your code and we'll see if we can spot where you went wrong?
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes sure,
In the properties fiel the code was

import java.util.ListResourceBundle;

public class MyProgramResource_hi extends ListResourceBundle{
public Object[][] getContents(){
System.out.println("Inside getContents Method");
return contents;
}
static Object[][] contents = {
{ "okKey","sahi he ya {0}"},{"cancelKey","galat"}
};
}

In the client class the program is as follows.

import java.util.ResourceBundle;
import java.util.Enumeration;
import java.util.Locale;
import java.text.MessageFormat;

class TestClient
{
public static void main(String[] args)
{
ResourceBundle bundle =
ResourceBundle.getBundle("MyProgramResource",new Locale("hi"));
Enumeration e = bundle.getKeys();
while(e.hasMoreElements()){
String key = (String)e.nextElement();
MessageFormat.format ((bundle.getString(key)),new String{"HI"});
}
}
}

Can you help me substituting the values for the two places as {0}.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ResourceBundle.getBundle("MyProgramResource",new Locale("hi"));

This is looking for a ResourceBundle called MyProgramResource for the Locale "hi" (not a standard Locale, what language are your really after?) . So you will need a properties file called MyProgramResource_hi.properties in your classpath. Did this line work? If the names properties file does not exist, you will get a MissingResourceException.

Assuming you did successfully get this ResourceBundle I would assume you have a defined property you want to format. So, rather than getting all defined keys in the ResourceBundle, why not get the one you are interested in:

If you want to substitute values, I would expect substitutionString to be a string like this:

If you have that, then you can apply the MessageFormat:
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanksa lot. Its working. And "hi" represents Hindi and the locale is India. Anyways the code that you send is working fine.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by PradeepBhatt Prasad:
Thanksa lot. Its working. And "hi" represents Hindi and the locale is India. Anyways the code that you send is working fine.


So it does! You live and learn. Glad you got it working!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!