In this recipe, we will modify the portlet to use property files for user interface text and create a German language version of them so that we can switch to the German locale and see the appropriate text displayed in the portlet.
The following are required for this recipe:
HelloWorldPortlet
project from the Creating a portlet with the Portlet 2.0 Specification recipeTo recognize the locale being used in the portal, and to support internationalized messages, do the following:
portlet.xml
after </supports>
:<supported-locale>en</supported-locale> <supported-locale>de</supported-locale> <resource-bundle>gatein.cookbook.chapter6.HelloWorldPortlet</resource-bundle>
HelloWorldPortlet.properties
and HelloWorldPortlet_de.properties
in the src/main/resources/gatein/cookbook/chapter6
folder of the project. HelloWorldPortlet.properties
:javax.portlet.title=Hello World Portlet javax.portlet.display-name=Hello World Portlet link.reset=Reset link.return=Return welcome.heading=Welcome to the Hello World portlet welcome.form.name=Name welcome.form.submit=Say Hello helloworld.heading=Hello! helloworld.text.hello=Hello helloworld.text.message=from your first portlet!
HelloWorldPortlet_de.properties
:javax.portlet.title=Hallo Welt Portlet javax.portlet.display-name=Hallo Welt Portlet link.reset=Zurücksetzen link.return=zurückkehren helloworld.heading=Hallo! helloworld.text.hello=Hallo helloworld.text.message=von Ihrem ersten portlet! welcome.heading=Willkommen in der Hallo Welt Portlet welcome.form.name=Name welcome.form.submit=Sag Hallo
welcome.jsp
with the following:<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <portlet:defineObjects/> <fmt:setBundle basename="gatein.cookbook.chapter6.HelloWorldPortlet" /> <div> <div class="portlet-section-header"><fmt:message key="welcome.heading" /></div> <br/> <div class="portlet-section-body"> <form action='<portlet:actionURL name="nameAction"/>' method="post"> <span class="portlet-form-label"><fmt:message key="welcome.form.name" />:</span> <input class="portlet-form-input-field" type="text" name="yourname"/> <input class="portlet-form-button" type="Submit" value="<fmt:message key="welcome.form.submit" />"/> </form> </div> <br/> </div>
helloWorld.jsp
with the following:<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <portlet:defineObjects/> <fmt:setBundle basename="gatein.cookbook.chapter6.HelloWorldPortlet" /> <div> <div class="portlet-section-header"><fmt:message key="helloworld.heading" /></div> <br/> <div class="portlet-section-body"><fmt:message key="helloworld.text.hello" /> <%= renderRequest.getParameter("yourname") %> <fmt:message key="helloworld.text.message" /></div> <br/> <a href="<portlet:renderURL portletMode='view' />"><fmt:message key="link.reset" /></a> <br/> </div>
> mvn clean package
chapter6-1.0.0.SNAPSHOT.war
, from the target
folder into the deployment folder where you unpacked the GateIn installation.portlet
should now look like the following screenshot:Step 1 modifies the portlet.xml
file to inform the portlet container that it supports the English and German locales, and that it can find the necessary text resources for each locale in the properties file specified by <resource-bundle>
.
Steps 2, 3, and 4 create the properties files for the English and German locales with the appropriate resource labels and values in each of them.
Steps 5 and 6 modify the existing JSPs to incorporate the changes required for supporting localized text in a portlet. Each of the changes we've made will be detailed below.
At the top of each file a taglib
definition was added that provides access to the JSTL formatting tags necessary for localizing JSP content.
Just below the taglib
definition the
fmt:setBundle
tag is used to specify the location of the properties file that should be used to retrieve text from.
Note that the basename
identifying the filename containing localized text does not include the .properties
extension.
The last changes were to replace any text in the portlet with <fmt:message key="" />
, with the key being a unique name within the properties files that specifies which piece of text to retrieve for each locale.
As the earlier screenshots showed, the name of the portlet within the portal menus remained Hello World, as that identifier is set on the navigation node within the portal and not from the portlet. To also localize the name of the navigation node, do the following:
3.15.29.119