Locale Example

portlet.xml:

<?xml version="1.0"?>
 
<portlet-app
    version="2.0"
    xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
>
    <portlet>
        <portlet-name>localetest</portlet-name>
        <display-name>LocaleTest</display-name>
        <portlet-class>com.test.LocaleTest</portlet-class>
        <init-param>
            <name>view-jsp</name>
            <value>/html/localetest/view.jsp</value>
        </init-param>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>VIEW</portlet-mode>
        </supports>
        <supported-locale>en_US</supported-locale>
        <supported-locale>es</supported-locale>
        <resource-bundle>com.test.nl.CompanyInfoPortletResource</resource-bundle>
        <portlet-info>
            <title>LocaleTest</title>
            <short-title>LocaleTest</short-title>
            <keywords></keywords>
        </portlet-info>
        <security-role-ref>
            <role-name>administrator</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>guest</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>power-user</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>user</role-name>
        </security-role-ref>
    </portlet>
</portlet-app>

Properties files:
CompanyInfoPortletResource_en_US.properties on package com.test.nl

companyInfo.companyName=Travel Tips International Inc
companyInfo.city=Southfield
companyInfo.country=USA

CompanyInfoPortletResource_es.properties on package com.test.nl

companyInfo.companyName=Travel Tips International Inc
companyInfo.city=Madrid
companyInfo.country=Spanol

Model:

package com.test.business;
 
public class Company {
    private String companyName;
    private String city;
    private String country;
 
    public String getCompanyName() {
        return companyName;
    }
 
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
 
    public String getCity() {
        return city;
    }
 
    public void setCity(String city) {
        this.city = city;
    }
 
    public String getCountry() {
        return country;
    }
 
    public void setCountry(String country) {
        this.country = country;
    }
 
}
package com.test.business;
 
import java.util.ResourceBundle;
 
public class LoadCompany {
 
    public static Company getCompany(ResourceBundle portletBundle) 
    {
        Company company = new Company();
        company.setCompanyName(portletBundle.getString("companyInfo.companyName"));
        company.setCity(portletBundle.getString("companyInfo.city"));
        company.setCountry(portletBundle.getString("companyInfo.country"));
 
        return company;
    }
 
}

Portlet:

package com.test;
 
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.test.business.Company;
import com.test.business.LoadCompany;
 
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
 
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
 
/**
 * Portlet implementation class LocaleTest
 */
public class LocaleTest extends GenericPortlet {
 
    public void init() {
        viewJSP = getInitParameter("view-jsp");
    }
 
    public void doView(
            RenderRequest renderRequest, RenderResponse renderResponse)
        throws IOException, PortletException {
 
        Locale locale = renderRequest.getLocale();
        renderRequest.setAttribute("locale", locale);
 
        ResourceBundle portletBundle = getPortletConfig().getResourceBundle(locale);
        Company company = LoadCompany.getCompany(portletBundle);
        renderRequest.setAttribute("company", company);
 
        include(viewJSP, renderRequest, renderResponse);
    }
 
    protected void include(
            String path, RenderRequest renderRequest,
            RenderResponse renderResponse)
        throws IOException, PortletException {
 
        PortletRequestDispatcher portletRequestDispatcher =
            getPortletContext().getRequestDispatcher(path);
 
        if (portletRequestDispatcher == null) {
            _log.error(path + " is not a valid include");
        }
        else {
            portletRequestDispatcher.include(renderRequest, renderResponse);
        }
    }
 
    protected String viewJSP;
 
    private static Log _log = LogFactoryUtil.getLog(LocaleTest.class);
 
}

View:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
 
<portlet:defineObjects />
 
<table>
<tr><td>Locale:</td><td>${locale}</td></tr>
<tr><td>Company City:</td><td>${company.city}</td></tr>
<tr><td>Company Country:</td><td>${company.country}</td></tr>
</table>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License