I’m creating a simple program with the possibility to change the language and I would convert the list of properties in the file myBundle.properties in a String array.
This is the file myBundle.properties:
#default
test1=Hello1
test2=Hello2
test3=Hello3
test4=Hello4
test5=Hello5
test6=Hello6
And this is the Java code:
import java.util.*;
class BundleTest {
BundleTest() {
String[] s = returnStringArray(Locale.ENGLISH);
for(int i=0; i<s.length; i++) {
System.out.println(s[i]);
}
}
private String[] returnStringArray(Locale language) {
try {
ResourceBundle labels = ResourceBundle.getBundle("myBundle", language);
Enumeration<String> keys = labels.getKeys();
Vector v = new Vector();
String key = null;
while (keys.hasMoreElements()) {
v.add(keys.nextElement());
}
String[] s = new String[v.size()];
for(int i=0; i<s.length; i++) {
s[i] = (String)v.elementAt(i);
}
return s;
} catch (MissingResourceException mre) {
System.out.println("Risorse della lingua non trovate!");
return null;
}
}
public static void main(String[] args) {
new BundleTest();
}
}
But, surprisingly, when I execute the program it returns me the strings in a casual order. Why have Enumeration this strange behavior?
bash-4.1$ java BundleTest
test1
test6
test4
test5
test2
test3
I do not know the exact details of the ResourceBundle class, but when looking at your code example, it seems that it has key/value pairs.
This suggests that it stores its content in an HashMap. (Again, as I do not know ResounrceBundle, this is a hunch)
HashMap keys (and values) are unordered as they are stored on such a way that the value can be easily found for a given key.