Reputation: 7
I am a beginner in java developer. I have wrote a java class file in eclipse for getting all key value pairs of spanish property file which as below:
public class ResoucreBundleproperties {
static void iterateKeys(Locale currentLocale) {
ResourceBundle labels =
ResourceBundle.getBundle("xxxx_ar_SP",currentLocale);
Enumeration<String> bundleKeys = labels.getKeys();
while (bundleKeys.hasMoreElements()) {
String key = (String)bundleKeys.nextElement();
String value = labels.getString(key);
System.out.println("key = " + key + ", " +
"value = " + value);
}
}
public static void main(String[] args) {
Locale[] supportedLocales = {
new Locale("SP","SPANISH"),
Locale.ENGLISH
};
iterateKeys(supportedLocales[0]);
System.out.println();
}
I got a output of xxxx.ar_SP.properties
with all keys and values.
Now I have another two more files of properties like yyyy_ar_SP.properties
and zzzz_ar_SP.properties
.
(pls tell me if any mistakes I done or u cant get me).
Question 1: now how will I get all these two property files (xxxx_ar_SP.properties,yyyy_ar_SP.properties,zzzz_ar_SP.properties
) in the same java class. Is that possible?
Question 2: how to convert the spanish property files to unicode escapes?
Upvotes: 0
Views: 330
Reputation: 2095
It looks like you have multiple property files for a specific locale. In order to load these into one Properties
object you can do the following:
static void loadProperties(Locale currentLocale, String... propertyFileNames){
for(String propertyFileName : propertyFileNames)
{
ResourceBundle labels =
ResourceBundle.getBundle(propertyFileName ,currentLocale);
Enumeration<String> bundleKeys = labels.getKeys();
while (bundleKeys.hasMoreElements()) {
String key = (String)bundleKeys.nextElement();
String value = labels.getString(key);
System.out.println("key = " + key + ", " +
"value = " + value);
props.put(key, value);
}
}
}
Where props
is a static java.util.Properties
object.
Not sure about question 2 I'll have to look around a bit.
Upvotes: 0
Reputation: 24885
You are doing it wrong.
You have a default properties file xxxxx.properties
, one language specific properties file xxxxx_es.properties
, and if you want regional variations, then you add the country prefix xxxxx_es_ES.properties
.
The resource bundle will use the locale you provided, or the default one, and will do:
1) If a file for the language or language+country is found, use it. It will use the more specific value for a property; if your locale is "es_ES" and you provided a value in "xxxxx_es_ES", that one is used, otherwise (if your locale is "es_AR" and no xxxxx_es_AR.properties
exists, or if your locale is es_ES
but you did not define the value in xxxxx_es_ES.properties
), then it will search the value in xxxxx_es.properties
.
2) If your locale is not found, it will use the default (no locale) properties file.
Upvotes: 1