Using the user locale to localize portlet content

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.

Getting ready

The following are required for this recipe:

  • Apache Maven
  • An IDE of your choice
  • GateIn-3.2.0.Final
  • HelloWorldPortlet project from the Creating a portlet with the Portlet 2.0 Specification recipe

How to do it...

To recognize the locale being used in the portal, and to support internationalized messages, do the following:

  1. Add the following into portlet.xml after </supports>:
      <supported-locale>en</supported-locale>
      <supported-locale>de</supported-locale>
      <resource-bundle>gatein.cookbook.chapter6.HelloWorldPortlet</resource-bundle>
  2. Create HelloWorldPortlet.properties and HelloWorldPortlet_de.properties in the src/main/resources/gatein/cookbook/chapter6 folder of the project.
  3. Add the following content to 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!
  4. Add the following content to 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
  5. Replace the content of 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>
  6. Replace the content of 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>
  7. Run the following in the root of the project directory to build the web archive:
    > mvn clean package
    
  8. Copy the generated web archive, chapter6-1.0.0.SNAPSHOT.war, from the target folder into the deployment folder where you unpacked the GateIn installation.
  9. Start the server and access the portal page created in Creating a portlet with the Portlet 2.0 Specification.
  10. Click on Change Language link at the top-right corner, select German, and click on Apply.
  11. The HelloWorld portlet should now look like the following screenshot:
    How to do it...
  12. Navigate around the portlet to ensure that all portlet text is showing in German.
  13. When finished, click on Sprache wechseln to change the portal language back to English.

How it works...

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

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.

There's more...

Localizing navigation node name

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:

  1. Start the server and log in as an administrator.
  2. Click on Site in the menu at the very top of the window.
  3. Click on Edit Navigation and the Navigation Management pop-up will appear.
  4. Select Hello World with a left mouse click and then right-click on Hello World to display the menu as shown in the following screenshot:
    Localizing navigation node name
  5. Select Edit this Node and a pop-up will appear as seen in the following screenshot:
    Localizing navigation node name
  6. Select the drop-down for Language and choose German.
  7. In the Label field enter Hallo Welt and click on Save and click Save again on the Navigation Management window.
  8. Sign out as an administrator and access the portal page created in Creating a portlet with the Portlet 2.0 Specification.
  9. Select Change Language in the top-right corner and select German then click on Apply.
  10. The navigation node of the portal for the portlet should now be correctly localized, as seen in the following screenshot:
    Localizing navigation node name

See also

  • The Creating a portlet with the Portlet 2.0 Specification recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.29.119