The com.prosyst.util.propertiesfile.ResourceBundle class allows using locale-specific resources in a way similar to the java.util.ResourceBundle.
The Resource Bundle utility uses less resources and is intended for use on small devices. In addition, is can be used for loading images for an application in the framework.
Resource bundles are grouped in families that share a common base name, for example "MyResources". Each family can contain as many as needed locale-specific members but it must have a default resource bundle that will be used in case a specific locale is not supported.
Resource bundles that belong to one family should contain the same items, translated for the locales represented by them. Resources for each resource bundle are stored in a separate .properties files as key/value pairs and are placed in the bundle JAR file. The name of each file must have the following structure:
<base_name>_<language>_<country>.properties
Following is an example of a resource bundle .properties file for the Dutch locale that is named MyResources_nl.properties and resides in the MY-INF directory of the bundle:
Sample .properties file for a resource bundle is presented below:
name=naam
street=straat
country=staat
image=http://www.flags.com/countryflags/dutch.png
If you need a locale-specific resource bundle, you can obtain a ResourceBundle instance by calling the getBundle(String name, String locale, Bundle loader) method.
To retrieve an object from a resource bundle, you should use the getObject(String key) method passing the key of the locale-specific object as an argument.
To get information about all locales that a resource bundle provides, call the getLocales(String name, Bundle loader) which returns a String [] of the available locales.
The methods of the ResourceBundle class take the following parameters:
The following source example illustrates getting the desired ResourceBundle instance and using it to obtain the value of a property:
package myresourcebundle.test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import com.prosyst.util.propertiesfile.ResourceBundle;
public class MyTestResources implements BundleActivator {
ResourceBundle resourcebundle = null;
Bundle bundle = null;
public void start(BundleContext bc) throws Exception {
// Getting the resource bundle for the Dutch locale
bundle = bc.getBundle();
resourcebundle = ResourceBundle.getBundle("MY-INF/MyResources", "nl", bundle);
// Obtaining all available locales for the resource bundle
String[] locales = ResourceBundle.getLocales("MY-INF/MyResources", bundle);
System.out.println("The locales for the resource bundle are: ");
for (int i = 0; i < locales.length; i++) {
System.out.println(locales[i]);
}
// Getting the locale that is used when loading the resource bundle
String locale = resourcebundle.getLocale();
}
public void stop(BundleContext bc) throws Exception {
}
}