Thursday, 25 June 2015

How to provide multilingual support in Liferay custom portlet

Providing multilingual support in Liferay is very easy. Just need to follow few simple steps and we are all done.

Here are the steps needs to be performed:

1)  Modify portlet.xml file to specify the resource bundle entry
2)  Create properties file for the locate we need to support
3)  Use liferay-ui tld to show message
4)  Deploy and verify changes

Here are the detailed steps:

1)  Modify portlet.xml file to specify the resource bundle entry

    Add the entry "<resource-bundle>content.Language</resource-bundle>" inside the portlet tag. Tag specifies the location of the
    language file.
   
2)  Create properties file for the locate we need to support

    Need to create a key=value pair of properties file based on the locale.
   
    Ex: Language_es.properties
        Language.properties
   
    If we select a region whose corresponding file is not exist then value available inside "Language.properties" will be default as default.
    If we select "Spanish" then the value of the key available inside "Language_es.properties" will be shown to user.
   
3)  Use liferay-ui tld to show message

    On JSP we need to import "liferay-ui" taglib as shown below
   
    <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
   
    Once tablib import is done we can use "liferay-ui" to show messages to the user. So label will be fetch from the language property file based
    on the locale selected.
   
    Here is the code snippet to use message:

    <liferay-ui:message key="message1" />  
   
   
4)  Deploy and verify changes  

    Yupee! we are done from the configuration part. Lets build and deploy latest war and check the JSP where we have used "liferay-ui" to show
    message. Add the Language portlet on the page and change the language(locale) to "Spanish" you will see value against the key mentioned inside "Language_es.properties"
    file.
   
    Ex: Here are the entries inside
    1)  Language_es.properties
        message1=I am spanish
    2) Language.properties
        message1=I am Default Message

Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics    

No comments:

Post a Comment