Tuesday, December 12, 2017

Localization support in Liferay

If your portlets target an international audience, we can localize your portlets’ user interfaces. Liferay support several localization. We can easily localize your application to support more than one language.

We can override existing language key and also add language key in hook. Configuration required in the hook as below.

<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">

<hook>
    <portal-properties>portal.properties</portal-properties>
    <language-properties>content/Language_en.properties</language-properties>
    <language-properties>content/Language_fr.properties</language-properties>
    <language-properties>content/Language_pt.properties</language-properties>
</hook>

Here above hook show three language(fr,pt and en) support.

Add above three language files in src/content folder.

We can also say in hook, how many languages support by your application.
For do so you need to customize portal.properties.
Add portal.properties in src folder of hook.
and add below lines.

locales.enabled=fr_FR,en_US,pt_PT
locales=fr_FR,en_US,pt_PT

Here above lines say, our application support three language fr, en and pt.

There are several location where need to use language key.
1) Jsp page
2) Java class
3) Javascript file
4) Web content template

1. Jsp page:
            We can  use <liferay-ui message> tag if you prefer to use taglib.Taglib definition can be found on /util-taglib/src/META-INF/liferay-ui.tld file.For this you need to import  liferay-ui taglib at jsp page start.

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

2. Java class:
             For java classes, you can get localized text / content by using com.liferay.portal.kernel.language.LanguageUtil class. There are a few methods there to get localized text, for example :

    LanguageUtil.get(locale, key);

3. Javascript file:

       Liferay  make our life easier by providing javascript API for this. We can just call

       Liferay.Language.get("your-language-key")

       This javascript API is defined in portal-web/docroot/html/js/liferay/language.js file.

4. Web content template:

       In velocity template you can use language key as below,

       #language('your-key')

Cheers !

Popular Posts