Localization in Liferay

Localization is a feature that provides an option to the application user to use a localized language. We can enable the localization feature for the Liferay portlets.

To enable localization in a Liferay portlet, 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.

In this article, we will learn how to implement localization in the Liferay portlet.

Version details: Liferay 6.2

Implementing the Liferay portlet localization

Create a folder called content inside plugin project’s src/ folder and add a file named Language.properties.

In the below example, we have created the required resource bundle and added few language properties to it.

The language file contains keys and language-specific values, as shown below.

name:Name

Each language our portlet supports will have its property file with the name Language_*.properties.

These property files can be created manually or by using the build Languages option of LDS(Right-click on plugin project > Liferay > Build Language).

We can also use a custom name for the property files.

Language_hi_IN.properties
Language_es.properties

Add the tag <resource-bundle> inside Portlet.xml file. This tag shows the location of our resource bundle. Also, add the 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 the following tag library.

<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<liferay-ui:message key="name" />

We can pass string values to the tag <liferay-ui:message> to display the customized messages.

//{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 the java.util.Locale and java.util.ResourceBundle classes 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 portlet title

We can modify portlet details like portlet title, short title, keywords, etc by adding a related language key to our custom property file as shown below.

javax.portlet.title=स्थानीयकरण
javax.portlet.short-title=स्थानीयकरण
javax.portlet.keywords=स्थानीयकरण, अंतर्राष्ट्रीयकरण

We can share a common resource bundle between multiple portlets inside a plugin project.

For this, make sure that the <resource-bundle> tag in the portlet.xml file of all portlets is mapped to a common resource folder.

Following are few localization properties we can use in portal-ext.properties.

//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 a Language portlet to the page/theme so that users can select supported language. Default configuration enables only a few languages in the portal.

Language support option can be enabled from the portal-ext property as shown above or through the control panel: Control Panel > Portal properties > Display settings, then select language from the available list.

Liferay portlet localization example

A simple Liferay portlet

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 %>"/>

Testing the application

Following are the outputs.

localization1
localization2

Conclusion

In this article, we learned how to implement the localization feature in the Liferay portlet.