Thursday, July 21, 2011

Localization Specific Logo Image : Liferay Portal

Often there are requirements to show customized logo image according to portal's current locale. In Liferay Portal there is no way achieve this but locale specific logo can applied by using the theme.

In theme, portal_normal.vm contains the following statement to display logo -

<a href="$company_url" title="#language("go-to") $company_name">
<span>$company_name</span>
</a>

Here you can place check for the locale and accordingly change the logo image -

<a href="$company_url" title="#language("go-to") $company_name">
#if($locale == "en_US")
<img src="englishlogoimage" />
#else
<img src="spanishlogoimage" />
#end
</a>

but now you can't manage the logo from the community settings as image urls are hardcoded in the theme. To make it manageable you can use web content to render the logo image.

Any comments or suggestions will be appreciated ....

 

Tuesday, July 12, 2011

Localized Calendar Date Picker

Following is the simple but very useful script to display localized calendar date picker-

Calendar defaultValueDate = CalendarFactoryUtil.getCalendar(timeZone, locale);
<liferay-ui:input-date
dayValue="<%= defaultValueDate.get(Calendar.DATE) %>"
dayParam="day"
disabled="<%= false %>"
firstDayOfWeek="<%= defaultValueDate.getFirstDayOfWeek() - 1 %>"
monthParam="month"
monthValue="<%= defaultValueDate.get(Calendar.MONTH) %>"
yearParam="year"
yearValue="<%= defaultValueDate.get(Calendar.YEAR) %>"
yearRangeStart="<%= defaultValueDate.get(Calendar.YEAR) %>"
yearRangeEnd="<%= defaultValueDate.get(Calendar.YEAR) + 50 %>"
/>
view raw date-picker hosted with ❤ by GitHub

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