If our portlets want to target international audience, we can localize portlet user interfaces. For this, we have to create language property files(resource bundles) for each language our portlet supports. We can translate these property file content manually or by using web services. Liferay portal contains basic language properties required for portal. This can be found in portal-impl/src/content/Language.properties file.

Here are the basic steps to implement localization of portlet in liferay:

  • Create a directory called content inside plugin project’s src/ directory and add a file named Language.properties. In below example, we have created required resource bundle and added few language properties to it. This language file contains key and related language value as shown below:
name:Name
  • Each language our portlet supports will have its own property file with name Language_*.properties. These property files can be created manually or by using build Languages option of LDS(Right click on plugin project > Liferay > Build Language).We can also use our own name to the property files too.
Language_hi_IN.properties
Language_es.properties
  • Add the tag <resource-bundle> inside Portlet.xml file. This tag shows where our resource bundle is located.  Also add tag <supported-locale> to each language portlet supports as shown below:
<resource-bundle>content.Language</resource-bundle>

<supported-locale>hi_IN</supported-locale>
<supported-locale>es</supported-locale>
  • Now in JSP files, we can access these language keys using the tag <liferay-ui:message key=”key” />. For this, we have to import following tag library:
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<liferay-ui:message key="name" />
  • The <liferay-ui:message> tag also supports passing strings as arguments to a language key as shown below:
//{0} is used to pass argument and 0 - order of argument.
message-text=Hello{0}!

//in JSP file:
<liferay-ui:message key="message-text" arguments="argument-val"/>
  • We can also use ResourceBundle to get fetch language data as shown below. Here we are getting locale object from current render request and passing it to getResourceBundle method of portletConfig to get particular resource bundle. We also have to import java.util.Locale and java.util.ResourceBundle as shown below:
<%@page import="java.util.Locale"%>
<%@page import="java.util.ResourceBundle"%>

Locale locale = renderRequest.getLocale();
ResourceBundle resource = portletConfig.getResourceBundle(locale);

String name= resource.getString("name");

Modifying Portlets title:

  • We can modify portlet related information like portlet title, short title, keywords,etc by adding related language key to our custom property file as shown below:
javax.portlet.title=स्थानीयकरण
javax.portlet.short-title=स्थानीयकरण
javax.portlet.keywords=स्थानीयकरण, अंतर्राष्ट्रीयकरण
  • To share common resource bundle between multiple portlets inside a plugin project, make sure that in Portlet.xml file <resource-bundle> tag of all portlets are mapped to common resource directory.
  • Following are few useful properties we can use in portal-ext.properties file related to localization:
//Enables un authenticated/guest user to get prefered language content:
locale.default.request=true

//To set default locale of portal:
company.default.locale=en_US

//Specify the locales that are enabled by default:
locales.enabled=hi_IN
  • To check our localized portlet, we can change the append the url with language code like: localhost:8080/hi_IN/. We can also add Language portlet to the page/theme, so that user can select supported language.
  •  By default, only few languages are enabled in portal. These can be enabled from portal-ext property as shown above or through control panel : Control Panel > Portal properties > Display settings, then select language from available list.
  • Finally here is a simple example:

Language.properties:

name:Name
age:Age
message-text=Hello {0}

Language_hi_IN.properties:

name:नाम
age:आयु
message-text:नमस्ते {0}

Portlet.xml:

<supported-locale>en_US</supported-locale>
<supported-locale>hi_IN</supported-locale>
<resource-bundle>content.Language</resource-bundle>

view.jsp:

<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%>
<%@page import="java.util.Locale"%>
<%@page import="java.util.ResourceBundle"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<portlet:defineObjects />

This is the <b>Localization</b> portlet in View mode.

<h5>Simple Example:</h5>
<p><liferay-ui:message key="name" /> : ASB</p>
<p><liferay-ui:message key="age" /> : 25</p>
=======================================
<h5>Example by using Resource bundle:</h5>
<% 
 Locale locale = renderRequest.getLocale();
 ResourceBundle resource = portletConfig.getResourceBundle(locale);
 String name = resource.getString("name");
%>
<p><%=name %></p>
=======================================
<%
 String a=" ASB";
%>
<h5>Example with argument:</h5>
<liferay-ui:message key="message-text" arguments="<%=a %>"/>

Following are the outputs:

localization1

localization2

Check out all Liferay related posts.

Note: Version used: Liferay 6.2 + Apache Tomcat 7.