How to do Internationalization(I18n) and Localization(L10n) in Liferay , WebSynergy

Hi .. I'm Back .. was busy exploring more on I18n of Liferay

This blog will describe how to do i18n in development environment of Liferay and WebSynergy(now on will be referred as Portal). Also while adding new Portlets to Portal how to leverage existing Portal UI tag lib and Language utility class for I18n in quicker and easier way.

Resource Bundle

Key Points

  • Portal has only One Resource Bundle for all the components . Refer my previous entry for more details
  • Location in workspace : portal-impl/content/Language.properties
  •  Location in Installation :
          • Unjar portal-impl.jar (it will be lib directory of workspace)
          • Language.properties file will be found in content directory
  • Either you can directly modify this Language.properties OR (most preferred way) to create Language-ext.properties and bundle in same jar. Because if you directly modify Language.properties then you need to manually take care of adding your changes during updated to latest default Language.properties which you will get in install.


Convention to add messages in Resource bundle

  1. This file follows same convention as defined for Liferay's default Language.properties liferay/trunk/portal-impl/content/Language.properties.
  2. Each property should key should be prefix by your portlet name .
  3. File is well-defined in to various section descried below
    1. Portlet Titles : Portlet titles of your portlet. - NEEDS TO VERIFY
      1. Portlet title key should be constructed as javax.portlet.title.PORTLETNAME , where PORTLETNAME is name which you have mentioned in portlet-name tag of your portlet.xml.
        ex. java.portlet.title.FriendsPortlet=Friends Widget
      2. This feature is yet to test (by me) so you can use Portlet specific resource bundle to define title.
    2. Category Titles : Category titles which appears in "Add Application" list
      1. It should be prefix by category.CATEGORYNAME where CATEGORYNAME is name of category.
      2. ex. category.admin=Admin
      3. this category key will be used in liferay-display.xml of portlet which are part of category
        1. ex. <display>
              <category name="category.cms">
                  <category name="category.alfresco">
                      <portlet id="1" />
                  </category>
              </category>
          </display>
    3. Model resources : Model resources should be prefix by model.
      1. ex. model.resource.com.liferay.portal.model.Group=Community
      2. ex. model.resource.com.liferay.portlet.blogs.model.BlogsEntry=Blogs Entry
    4. Action Items : Messages which used as action items. Should be prefix by action.. All words are in uppercase. Word seperator is _
      1. ex. action.ADD_ARTICLE=Add Article
    5. Messages : General messages
      1. Key should be construed from Message. ex Key for message My Friends will be my-friends
      2. Use x for dynamic parameter in messages ex. Key for message This {0} is great will be this-x-is-great
      3. If message is very long (ex. more then 15 words) you can use small key but make sure it is giving information about message
        ex. an-applet-version-of-the-editor-is-also-available=An applet version of the editor is also available. It is a heavier but more user friendly editor that provides colorized text, search and replace, and other functionality. You can choose to use that editor by editing your portlet preferences.
  4. Keep each section of file in ascending order

How to use liferay ui taglib to fetch messages in JSP/Java Script

There are various ways to fetch message from resource bundle.

NOTE : All the following approach will consider Language.properties as Resource bundle and will fetch messages form there based on locale. However following methods support -ext mechanism i.e It will first look in Language-ext.properties file.

  1. liferay-ui:message tag
  2. LanguageUtil class
  3. UnicodeLanguageUtil class
  4. Other liferay ui tags - I18n'ed

liferay-ui:message tag

  • For simple messages which does not have dynamic parameter use this tag
    ex. < liferay-ui:message key="my-friends"/ >
  • This tag internally calls LangaugeUtil class

LangaugeUtil class

LanguageUtil class has various formatter methods use them based on situation.

  • For messages which has dynamic parameter use following method. Make last argument as "true" if you want your parameter to be translated as well
    ex. <%= LanguageUtil.format(pageContext, "upload-a-gif-or-jpeg-that-is-x-pixels-tall-and-x-pixels-wide", new Object {"120", "100"}, false) %>
  • For messages which has html code along with parameter then use LangaugeWrapper as argument instead of Object
    ex.<%= LanguageUtil.format(pageContext, "your-account-with-login-x-is-not-active", new LanguageWrapper {new LanguageWrapper("", user.getFullName(), ""), new LanguageWrapper("< b >< i >", userLogin, "< /b >")}, false) %>

Other liferay ui tags and LanguageUtil class methods- I18n'ed

  • liferay-ui:icon-help - This tag is i18n'ed. You can use key value in message attribute of this tag
    • < liferay-ui:icon-help message="allow-dictionary-words-help"/ >
  • LanguageUtil.getTimeDescription - to get localized time descrption
    • <%= LanguageUtil.getTimeDescription(pageContext, _DURATIONSi? \* 1000) %>

UnicodeLanguageUtil Class

  • Use this class in Java Script in JSP pages
  • UnicodeLanguageUtil has similar method singature as found in LanguageUtil class.
  • ex. function copyFromLive() {

if (confirm('<%= UnicodeLanguageUtil.get(pageContext, "are-you-sure-you-want-to-copy-from-live-and-overwrite-the-existing-staging-configuration") %>')) { document. fm. <%= Constants.CMD %>.value = "copy_from_live"; submitForm(document. fm); } }

  • Also use in alert within jsp
  • ex2.
    " method="post" name=" fm" onSubmit="alert('<%= UnicodeLanguageUtil.get(pageContext, "please-be-patient") %>'); submitForm(this); return false;">

Import and define tag lib in your jsp or included jsp

  • <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
  • Import following classes
    • <%@ page import="com.liferay.portal.kernel.language.LanguageUtil" %>
    • <%@ page import="com.liferay.portal.kernel.language.LanguageWrapper" %>
    • <%@ page import="com.liferay.portal.kernel.language.UnicodeLanguageUtil" %>

How to use liferay Language Utill classes to fetch messages in JAVA programs

In order to fetch localized messages in java program we will be using same Util classes which has been used in JSP/Java Script

  • LanguageUtil class
  • If your message does not have any dynamic parameter then use any of LanguageUtil.get(...,key) method. this method will fetch message assicated with key. There are various overloaded version of LanguageUtil.get are available , use desired get method based on situation.  Some of commonely used version of get method.
    • LanguageUtil.get(pageContext,key)
    • LanguageUtil.get(companyId,key) etc.
    • there are other overloaded version of get method is available. You need to use them based on your requirement.
    • Refer class "portal/trunk/portal-kernel/src/com/liferay/portal/kernel/language/LanguageUtil.java for details.
  • If your message has  dynamic parameter then use any of LanguageUtil.format(...,key,Object) method.  this method will fetch message assicated with key. There are various overloaded version of LanguageUtil.form are available , use desired format method based on situation.  Some of commonely used version of form method.
    • LanguageUtil.get(pageContext,key, object)
    • LanguageUtil.get(pageContext,key, new Object[])
    • there are other overloaded version of format method is available. You need to use them based on your requirement.
    • Refer class "portal/trunk/portal-kernel/src/com/liferay/portal/kernel/language/LanguageUtil.java for details.

How to use liferay Language Utill classes to fetch messages in Velocity templates

Portal uses velocity engine to create templates i.e. .vm files which are used to define layout and in themes.

  • Set Language Id : #set ($language_id = $user.getLanguageId()) in your init template.
  • You can use following syntex to fetch messages from Language.properties file
    • #language ("KEY-NAME")
  • Some places I have also seen usage of language util as follow.
    • $languageUtil.get($company_id, $locale, "add-application"))

There is also certain changes required in portlet.xml <resource-bundle> tag to allow container to fetch portlet title from common resource bundle. Watch out for this space , I will update it shortly. 

 Never shy away from re using stuff which already exists ... It gives more time to innovate...

Comments:

Hi!

And what about if I only wanna include a message from a .properties file in a velocity template? I'm looking for something like:

< liferay-ui:message key="my-friends"/ >

but for velocity files. Do you know how can I do it?

Thanx. Great, clean and clever information.

Posted by LordHASH on October 07, 2008 at 07:57 AM IST #

Hi LordHASH,

Regarding velocity templates I had not done great reasearch. But information under section "How to use liferay Language Utill classes to fetch messages in Velocity templates" might be useful to you.

Or you looking for something else.

Posted by Mahipalsinh Rana on October 17, 2008 at 07:17 AM IST #

I got a simple query.
How can i do i18n for the custom portlet names?

Posted by Rahul on October 22, 2008 at 11:25 PM IST #

Hi!

I have some queries regarding the portlets installed as a war file, If I am keeping the Language.properties under the WEB-INF/classes/content folder liferay's API (LanguageUtil) is not able to fetch the properties, It was working fine in older versions of liferay(4.3.6, 4.4.2).

Posted by Faisal on November 01, 2008 at 03:25 AM IST #

vcb

Posted by guest on November 18, 2008 at 10:02 AM IST #

vcb

Posted by guest on November 18, 2008 at 10:02 AM IST #

Hi
I'm trying to use the tag <liferay-ui:message key="test" />
to access a text wich i enterd in the CMS/journal portlet. But i get
java.lang.IllegalArgumentException: argument type mismatch

file looks like this:

<jsp:root version="2.1" xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ice="http://www.icesoft.com/icefaces/component"
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:liferay-ui="http://liferay.com/tld/ui">

<jsp:directive.page import="com.liferay.portal.kernel.language.LanguageUtil"/>
<jsp:directive.page import="com.liferay.portal.kernel.language.LanguageWrapper"/>
<jsp:directive.page import="com.liferay.portal.kernel.language.UnicodeLanguageUtil"/>
<jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>

<jsp:directive.page import="com.liferay.portal.kernel.util.ParamUtil"/>
<jsp:directive.page import="com.liferay.portlet.tags.model.TagsAsset"/>
<jsp:directive.page import="com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil"/>
<jsp:directive.page import="com.liferay.portlet.tags.NoSuchAssetException"/>

...

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

Posted by Andreas on December 03, 2008 at 09:44 AM IST #

Hi Faisal,
Make sure that you have added the element Faisal <supported-locale>xx</supported-locale> to your portlet.xml file.

Posted by Ray on April 13, 2009 at 07:46 PM IST #

Does anyone know a decent i18n editor?

Posted by Jane Bronsik on September 11, 2009 at 11:07 AM IST #

hi, thanks for the article. I'm trying to use LanguageUtil in my portlet Action class. I'm using Liferay 5.2.5.
I added an import:
import com.liferay.portal.language.LanguageUtil;

I get a compile error "cannot find symbol" for this class.

[javac]
action\\SubmitContactAction.java:34: cannot find symbol
[javac] symbol : class LanguageUtil
[javac] location: package com.liferay.portal.language
[javac] import com.liferay.portal.language.LanguageUtil;

Also, how does one get the PageContext to pass to LanguageUtil?

Posted by denis osullivan on September 18, 2009 at 06:20 AM IST #

I'm trying to use the Liferay.Language.get(key) in the .js file but it is returning me the key only.
Is there something else I need to do in order to get the message in the .js file.

Thanks

Posted by Rahul on May 05, 2010 at 02:20 PM IST #

Hi Mahipalsinh,
Thanks for a good article.

This is an old article, I am trying my luck for the reply...

You said you have to verify the 'Portlet Titles'...
I followed your instructions.. I have a war file and packaged Language-ext_en.properties and Language-ext_ar.properties in WEB-INF/classes/content, having javax.portlet.title.MYID property.

My portlet.xml relevant contents
portlet name is MYID and the portlet title is Book Catalog

The other properties reflect in the deployed portlet when I change the language but the title does not. Could you please direct me where I am going wrong.
Thanks in advance.

Posted by Satish on August 05, 2010 at 08:42 AM IST #

dfs

Posted by wer on October 12, 2010 at 09:03 AM IST #

Post a Comment:
Comments are closed for this entry.
About

Mahipalsinh Rana

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today