Saturday, September 17, 2016

Localize Portlet Title in Liferay

In the previous tutorial Localization (l10n) in liferay we change the portlet content on the basis of locale.But what if we want to change the title of portlet.In this tutorial we localize title of portlet.We can achieve this by two ways:-


First Method

Put a key (ex-portlet_title) in your Language_xxx.properties file and get the key using default locale and than set in setTitle() method:-

Demo.java
package com.demo;
import java.io.IOException;
import java.util.Locale;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.util.JavaConstants;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class Demo extends MVCPortlet {
@Override
public void doView(RenderRequest renderRequest,RenderResponse renderResponse) throws IOException, PortletException {
Locale defaultLocale = Locale.getDefault();
PortletConfig portletConfig = (PortletConfig) renderRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
String title = LanguageUtil.get(portletConfig, defaultLocale, "portlet_title");
renderResponse.setTitle(title);
super.doView(renderRequest, renderResponse);
}
}
view raw Demo.java hosted with ❤ by GitHub





Second Method

This is pretty simple just put the key as javax.portlet.title and that's it. No need to do anything in Controller. Ex:- 

javax.portlet.title = MyCustomTitleAgain

Output:-


No comments:

Post a Comment

Total Pageviews

1041228

Number Of Unique Visitor

Free counters!