Using PortletLocalizedContextHome, we can obtain PortletLocalizedContext and ResourceBundleProvider object.
PortletLocalizedContext - methods to obtain title and description of a portal resource inside a portlet and provides convenience methods for that portlet
ResourceBundleProvider -Allows to load a resource bundle when the set of supported localed for the bundle is known.
PortletLocalizedContextHome Method Summary | |
getLocalizedContext(PortletRequest aRequest) Returns a context instance which uses the language ranges specified in the given locale enumeration. | |
getLocalizedContext(PortletRequest aRequest, MimeResponse aResponse) Returns a context instance which uses the language ranges specified in the given locale enumeration. | |
getResourceBundleProvider(PortletConfig config) Loads the ResourceBundleProvider for the given PortletConfig. |
PortletLocalizedContext Method Summary | |
java.util.Map.Entry<java.util.Locale,java.lang.String> | getLocalizedDescription() Returns a fitting description of the portlet window based on the information the context was created with. |
java.util.Map.Entry<java.util.Locale,java.lang.String> | getLocalizedKeywords() Returns a fitting keywords of the portlet window based on the information the context was created with. |
java.util.Map.Entry<java.util.Locale,java.lang.String> | getLocalizedShortTitle() Returns a fitting short title of the portlet window based on the information the context was created with. |
java.util.Map.Entry<java.util.Locale,java.lang.String> | getLocalizedTitle() Returns a fitting title of the portlet window based on the information the context was created with. |
getObjectID() Returns the ObjectID of the portlet window | |
java.util.ResourceBundle | getResourceBundle() Gets the resource bundle defined by the portlet based on the information the context was created with. |
IterableListModel<java.util.Locale> | getSupportedLocales() Returns the list of locales supported by the portlet. |
ResourceBundleProvider Method Summary | |
java.util.ResourceBundle | getBundle(java.util.Locale locale) Returns a resource bundle in the given locale |
In this example, I have declared the portlet to support en and de language. But I have declared only properties file for en only.
In portlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" id="com.ibm.myportletlocalizedcontexthome.MyPortletLocalizedContextHomePortlet.7271fc6a23">
<portlet>
<portlet-name>MyPortletLocalizedContextHome</portlet-name>
<display-name xml:lang="en">MyPortletLocalizedContextHome</display-name>
<display-name>MyPortletLocalizedContextHome</display-name>
<portlet-class>com.ibm.myportletlocalizedcontexthome.MyPortletLocalizedContextHomePortlet</portlet-class>
<init-param>
<name>wps.markup</name>
<value>html</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
<supported-locale>en</supported-locale>
<supported-locale>de</supported-locale>
<resource-bundle>com.ibm.myportletlocalizedcontexthome.nl.MyPortletLocalizedContextHomePortletResource</resource-bundle>
<portlet-info>
<title>MyPortletLocalizedContextHome</title>
<short-title>MyPortletLocalizedContextHome</short-title>
<keywords>MyPortletLocalizedContextHome</keywords>
</portlet-info>
</portlet>
<default-namespace>http://MyPortletLocalizedContextHome/</default-namespace>
</portlet-app>
MyPortletLocalizedContextHomePortletResource_en.properties
# en Resource Bundle
#
# filename: MyPortletLocalizedContextHomePortletResource_en.properties
# Portlet Info resource bundle example
javax.portlet.title=MyPortletLocalizedContextHome
javax.portlet.short-title=MyPortletLocalizedContextHome
javax.portlet.keywords=MyPortletLocalizedContextHome
javax.portlet.description=Provides details about portlet.xml
Sample Code:
package com.ibm.myportletlocalizedcontexthome;
import java.io.*;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.portlet.*;
import com.ibm.bean.LocalizedBean;
import com.ibm.bean.PortletLocalizedContextHomeBean;
import com.ibm.bean.ResourceBean;
import com.ibm.portal.IterableListModel;
import com.ibm.portal.model.ResourceBundleProvider;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.model.PortletLocalizedContext;
import com.ibm.portal.portlet.service.model.PortletLocalizedContextHome;
/**
* A sample portlet based on GenericPortlet
*/
public class MyPortletLocalizedContextHomePortlet extends GenericPortlet {
public static final String JSP_FOLDER = "/_MyPortletLocalizedContextHome/jsp/"; // JSP folder name
public static final String VIEW_JSP = "MyPortletLocalizedContextHomePortletView"; // JSP file name to be rendered on the view mode
/**
* @see javax.portlet.Portlet#init()
*/
PortletServiceHome psh;
public void init() throws PortletException{
super.init();
try {
Context ctx = new InitialContext();
psh = (PortletServiceHome) ctx.lookup(PortletLocalizedContextHome.JNDI_NAME);
} catch(NameNotFoundException ex) {
ex.printStackTrace();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Serve up the <code>view</code> mode.
*
* @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
*/
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
// Set the MIME type for the render response
response.setContentType(request.getResponseContentType());
PortletLocalizedContextHome home = (PortletLocalizedContextHome) psh.getPortletService(PortletLocalizedContextHome.class);
PortletLocalizedContext ctx = home.getLocalizedContext(request);
LocalizedBean localized=new LocalizedBean();
Map.Entry<Locale,String> title=ctx.getLocalizedTitle();
Map.Entry<Locale,String> shorttitle=ctx.getLocalizedShortTitle();
Map.Entry<Locale,String> description=ctx.getLocalizedDescription();
Map.Entry<Locale,String> keywords=ctx.getLocalizedKeywords();
ResourceBundle resource=ctx.getResourceBundle() ;
Map<Locale,String> mapresource=new HashMap<Locale,String> ();
mapresource.put(resource.getLocale(),resource.getString("javax.portlet.keywords"));
localized.setTitle(title);
localized.setShorttitle(shorttitle);
localized.setDescription(description);
localized.setKeywords(keywords);
localized.setMapresource(mapresource);
ArrayList<String> supportedlocales=new ArrayList<String>();
IterableListModel<java.util.Locale> locale=ctx.getSupportedLocales();
ResourceBundleProvider rprovider=home.getResourceBundleProvider(getPortletConfig());
ResourceBean resourcebean=new ResourceBean();
Map<String ,String> providermap=new HashMap<String ,String>();
Iterator<java.util.Locale> itr=locale.iterator();
while(itr.hasNext())
{
Locale loc=(Locale)itr.next();
supportedlocales.add(loc.getDisplayName());
ResourceBundle res=rprovider.getBundle(loc);
if(null!=res){
providermap.put(loc.getDisplayLanguage(),res.getString("javax.portlet.keywords"));
}
else
{
providermap.put(loc.getDisplayLanguage()," value is empty");
}
}
localized.setSupportedlocales(supportedlocales);
resourcebean.setProvidermap(providermap);
PortletLocalizedContextHomeBean portletlocalizedbean=new PortletLocalizedContextHomeBean();
portletlocalizedbean.setLocalized(localized);
portletlocalizedbean.setResourcebean(resourcebean);
request.setAttribute("result", portletlocalizedbean);
// Invoke the JSP to render
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
rd.include(request,response);
}
/**
* Returns JSP file path.
*
* @param request Render request
* @param jspFile JSP file name
* @return JSP file path
*/
private static String getJspFilePath(RenderRequest request, String jspFile) {
String markup = request.getProperty("wps.markup");
if( markup == null )
markup = getMarkup(request.getResponseContentType());
return JSP_FOLDER + markup + "/" + jspFile + "." + getJspExtension(markup);
}
/**
* Convert MIME type to markup name.
*
* @param contentType MIME type
* @return Markup name
*/
private static String getMarkup(String contentType) {
if( "text/vnd.wap.wml".equals(contentType) )
return "wml";
else
return "html";
}
ScreenShot
Click here to download the source code
No comments:
Post a Comment