Access Resource Bundle in XPages from JAVA
Today, I tried to figure out how to access a resource bundle and return a value for a given key. I have the extensionLibrary installed and so I’m using some of the methods that are provided by the ExtLibUtils class.
To make the class available in your application, add the following line to your JAVA code.
import static com.ibm.xsp.extlib.util.ExtLibUtil.*;
The next lines of code gives you access to a global variable. When you make use of a resource bundle, a global variable is returned and you can access the bundle via this variable.
public Object getGlobalObject(String obj) throws Exception {
return resolveVariable(FacesContext.getCurrentInstance(),obj);
}
And here is the funktion that extracts the value from the key/value pair in the resource:
public String getLangString(String bundle,String key) throws Exception {
try{
ResourceBundle rb = (ResourceBundle)this.getGlobalObject(bundle);
return rb.getString(key);
}catch (Exception e) {
e.printStackTrace();
return "##NOT FOUND##";
}
}
You now can use the method to return a value for a given key from a specific resoure bundle
getLangString('myButtonLabels','CANCEL');
If there is an alternative way, pls. let me know. I’m sure there is. …