Access Resource Bundle in XPages from JAVA

January 21, 2012 – 2:35 pm

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. …

  1. 4 Responses to “Access Resource Bundle in XPages from JAVA”

  2. The XSPContext object has a bundle method you can use to access properties e.g:

    XSPContext xspContext = XSPContext.getXSPContext(FacesContext.getCurrentInstance());
    try {
    ResourceBundle bundle = xspContext.bundle(“application”);
    String appName = bundle.getString(“appName”);
    } catch(IOException e) {
    e.printStackTrace();
    }

    By Andy Cunliffe on Jan 21, 2012

  3. Thx, Andy. Right, you can do it this way. But you have to pass the bundle name ( de_myButtonLabels.properties ) to the method.

    What I want to access is the global var that is returned from the resource definition

    Sample:

    xp:bundle var="myButtonLabels"

    We set the bundle according to the user language in our application. So we need to access the var=”myButtonLabels” instead of accessing the resource by name.

    By Ulrich Krause on Jan 21, 2012

  4. Hi Ulrich,

    I don’t know xpages, but jsf and why IBM Lotus would change the default behaviour.
    I think this is about accessing the value for a key in the ressource bundeles in a managed bean and not in the view templating stuff:

    IllegalArgumentException is an unchecked exception you don’t have to catch. I use those a lot for acceptale arguments in methods.

    public static String retrieveI18nMessage(FacesContext ctx, String msgKey) throws KeyNotFoundInBundleException {
    if (ctx == null) throw new IllegalArgumentException(“ctx can’t be null”);
    if (msgKey == null) throw new IllegalArgumentException(“msgKey can’t be null”);

    Locale loc = ctx.getViewRoot().getLocale();
    ResourceBundle bundle = ResourceBundle.getBundle(ctx.getApplication().getMessageBundle(), loc);
    String msg = bundle.getString(msgKey);
    if (msg == null) {
    throw new KeyNotFoundInBundleException(“No key for ” + msgKey + ” in locale ” + loc.getDisplayName());
    }
    return msg;
    }

    and to access:
    public static String getI18nString(msgKey) {
    FacesContext ctx = FacesContext.getCurrentInstance();
    String i18nValue = “#” + msgKey + “#”;
    try {
    i18nValue = retrieveI18nMessage(FacesContext ctx, String msgKey);
    }
    catch (KeyNotFoundInBundleException e) {
    e.printStackTrace();
    }
    return i18nValue;
    }

    To check if there is a value for the key in the locale I would use a checked Exception generated by Eclipse:
    public class KeyNotFoundInBundleException extends Exception {

    public KeyNotFoundInBundleException() {
    // TODO Auto-generated constructor stub
    }

    public KeyNotFoundInBundleException(String arg0) {
    super(arg0);
    // TODO Auto-generated constructor stub
    }

    public KeyNotFoundInBundleException(Throwable arg0) {
    super(arg0);
    // TODO Auto-generated constructor stub
    }

    public KeyNotFoundInBundleException(String arg0, Throwable arg1) {
    super(arg0, arg1);
    // TODO Auto-generated constructor stub
    }

    }

    By axel on Jan 23, 2012

  5. In the view templating stuff it should be much easier.
    This may help: http://jsflessons.blogspot.com/p/internationalization.html
    JSF takes the locale from the prefered locale of the browser. But you can overwrite this behaviour with session scoped managed beans.

    By axel on Jan 23, 2012

Sorry, comments for this entry are closed at this time.