Saturday, July 9, 2011

Community Specific Locale Liferay Portal (Per Community BasisLocalization)

Liferay provides us with fine grained built-in localization features. In Liferay Portal one can apply localization on per user basis. But how to apply default locale to a Community, Liferay doesn’t provide any such functionality but it can be implemented using Portal Properties Hook.

So case is, portal instance have more than one Community and we need to apply a default language setting for one Community that is different from other Communities. To achieve this implement an Event Hook for servlet.service.events.pre.

Following is the step by step description to achieve this functionality-

1.  Create a portal properties hook and choose the servlet.service.events.pre Event

2.  Create a new class say SampleClass by extending the com.liferay.portal.kernel.events.Action

package com.sample.SampleClass;
public class SampleClass extends Action {
private static final String LOCALE_KEY = "org.apache.struts.action.LOCALE";
public SampleClass() {
super();
}
public void run(HttpServletRequest request, HttpServletResponse response)
throws ActionException {
try {
HttpSession session = request.getSession();
// replace the LONG_GROUP_ID with the group id to which apply the
// localization
if ((Long) session.getAttribute("LIFERAY_SHARED_VISITED_GROUP_ID_RECENT") == LONG_GROUP_ID)
{
boolean refresh = false;
Locale sessionLocale = (Locale) session.getAttribute(LOCALE_KEY);
String cookieLanguageId = CookieUtil.get(request,"GUEST_LANGUAGE_ID");
String currentURL = PortalUtil.getCurrentURL(request);
// provide the desired language settings value for the
// defaultLanguageId
String defaultLanguageId = "en_US";
Locale defaultLocale = LocaleUtil.fromLanguageId(defaultLanguageId);
if (cookieLanguageId == null || (!cookieLanguageId.equals(defaultLanguageId)))
{
LanguageUtil.updateCookie(request, response, defaultLocale);
refresh = true;
}
if (sessionLocale == null || (!sessionLocale.equals(defaultLocale)))
{
session.setAttribute(LOCALE_KEY, defaultLocale);
refresh = true;
}
if (refresh) {
response.sendRedirect(currentURL);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
view raw SampleClass hosted with ❤ by GitHub

3.  Now en_US locale will be applied to LONG_GROUP_ID Community.

Any queries or comments.........

1 comment:

  1. That's useful.

    Do you know how to redirect a user after login to a specific community based on his mail domain?

    user@microloft.com - Is redirected to Microlot comunity
    user@redapple.com - Is redirected to Red Apple comunity

    Than you!

    ReplyDelete