12.1.3 Accessing resource bundles in portlets

If you are printing out content directly from the portlet, you can use the portlet API to access the resource bundles quite easily. Most of your development will adhere to a good MVC approach; you can use this approach for setting the title, predefining parameters in a PortletURI or if you are providing some content via the beginPage or endPage methods.

The resource bundle is accessed via the PortletContext object's getText method as displayed in Example 12-3.

Example 12-3 getText API

PortletContext.getText("Bundle Base Name", "Key", Locale)

Bundle Base Name: the first parameter indicates the base name of the resource bundle. The name includes the path relative to the classes directory as shown in Example 12-4. The name does not specify the locale suffix or the properties file type. If the base file name cannot be found, or the key is not present in the properties file, a PortletException will be thrown.

Key: this parameter maps to a key value in the properties file. If the key is not found, a PortletException is thrown.

Locale: this is used by the Portal to create the complete resource bundle name. You are free to use any locale you like but to ensure the user's locale is returned, the code in Example 12-4 works well. The getLocale method returns the preferred locale for the user. The Portal Server determines the locale by first retrieving the user's preferred language set during registration. If the preferred language is not set, the locale is retrieved from the accept-language header supplied by the client.

Example 12-4 Accessing resource bundles via the API

getPortletConfig().getContext().getText("nls.NLSExample", "welcome", request.getLocale());


Redbooks
ibm.com/redbooks