03-29-2012 03:36 AM
Hi
How do I get a String array Resource using a string key...
for Example I have 4 String arrays defined in the resources.
d1, d2, d3, d4
How can I get these resource like.
private static ResourceBundle nameResources = ResourceBundle.getBundle( BUNDLE_ID, BUNDLE_NAME);
instead of giving keyname d1 can I somehow give "d"+1 (so that I can loop resources)?
Solved! Go to Solution.
03-29-2012 03:45 AM
03-29-2012 03:57 AM
03-29-2012 04:10 AM
03-29-2012 04:21 AM - edited 03-29-2012 04:25 AM
It seems that you wish to have the benefit of using the localization, but also to make the code that chooses the localization key dynamic.
You will have to introduce this dynamic change yourself. Here is an example (not your exact example, but something I hope you can extend to meet your needs).
Assume that you have a choice of 3 messages, and the one you choose us determined by the messageIndex variable you set which can have the value 0, 1 or 2. The messages are in the resources and have the ids
MESSAGE_ZERO
MESSAGE_ONE
MESSAGE_TWO
In your program, you can do the following:
private static ResourceBundle nameResources = ResourceBundle.getBundle( BUNDLE_ID, BUNDLE_NAME);
private static String [] messages = new String {
nameResources.getString(MESSAGE_ZERO),
nameResources.getString(MESSAGE_ONE),
nameResources.getString(MESSAGE_TWO)
}
....
String thisMessage = messages[messageIndex];
Edit:
If you wished to extend this so that you could use a String as the 'index', then the easiest option would be to replace the messages array in the sample above with a Hashtable, and populate the Hashtable using
- the Strings that you want to search on as keys, and
- the values extracted from the resource bundle as the data.
Hope this makes sense.