Book Image

SPRING COOKBOOK

Book Image

SPRING COOKBOOK

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Building multilingual pages


Next we will learn how to create a multilingual page (in English and French) using only one JSP, and display it in English by default, with a link to switch to French. We will then store the text outside the JSP, in both languages, in separate .properties files.

How to do it…

Here are the steps to build a bilingual JSP view:

  1. Create the JSP:

    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    
    <html>
    <body>
      <h1><spring:message code="home.title" /></h1>
      <p><spring:message code="home.intro" /></p>
    
      <p>
        <a href="?lang=en">English</a> |
        <a href="?lang=fr">French</a>
      </p>
    </body>
    </html>
  2. Create the English .properties file src/main/resources/messages.properties:

    home.title=Home
    home.intro=This is a magnificent home page, isn't it?
  3. Create the French .properties file src/main/resources/messages_fr.properties:

    home.title=Accueil
    home.intro=Splendide...