Java/JSP/I18N

Материал из Java эксперт
Версия от 07:10, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Current Locale

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
<head><title><fmt:message key="Welcome" /></title></head>
<body>
<h2><fmt:message key="Hello" /> <fmt:message key="and" /> <fmt:message key="Welcome" /></h2>

Locale: <c:out value="${pageContext.request.locale.language}" />_<c:out value="${pageContext.request.locale.country}" /> 
<br /><br />
<fmt:formatNumber value="1000000" type="currency" />
    
</body>
</html>





Internationalized Web Applications: JavaServer Pages

/*
Java Internationalization
By Andy Deitsch, David Czarnecki
ISBN: 0-596-00019-7
O"Reilly
*/
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class JSPLocaleNegotiatorServlet extends HttpServlet {
  private static final String defaultJSP = "/jsp/default_en_US.jsp";
  private static final String jspExtension = ".jsp";
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      IOException, ServletException {
    Locale usersLocale = request.getLocale();
    StringBuffer jspWithLocale = new StringBuffer();
    if (request.getParameter("jsppage") != null) {
      jspWithLocale.append(request.getParameter("jsppage"));
      jspWithLocale.append("_");
      jspWithLocale.append(usersLocale.toString());
      jspWithLocale.append(jspExtension);
    } else
      jspWithLocale.append(defaultJSP);
    response.setLocale(usersLocale);
    getServletConfig().getServletContext()
        .getRequestDispatcher(jspWithLocale.toString())
        .forward(request,response);
  }
}





JSP: Define the string in tag (I18N)

/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/
<%@ taglib prefix="wroxtags" tagdir="/WEB-INF/tags" %>
<html>
  <head>
    <title>Book Information</title>
  </head>
  <body>
    <h1>Book Information</h1>
    <hr>
    <p>The name of this book is <i><wroxtags:bookTitle/></i>.</p>
    <p>It is published by <wroxtags:publisher/>.</p>
    <p>The server used in all the examples is <wroxtags:containerName/>.</p>
    </body>
</html>





JSP Internationalization and Localized Content 1

/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/





JSP Internationalization and Localized Content 2

/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/





JSP Internationalization and Localized Content:Currency Formatting and locales

/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/





JSP Internationalization and Localized Content: Date Formatting and locale

/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/





Locale Display in a JSP

<%@ taglib uri="http://java.sun.ru/jsp/jstl/core" prefix="c" %>
<html>
<head><title>Locale Display</title></head>
<body>
<h2>Here is your preferred locale info...</h2>
<c:set var="clientLocale" value="${pageContext.request.locale}" />
<c:set var="clientLocales" value="${pageContext.request.locales}" />
Preferred locale: ${clientLocale.displayName}
 <br />
Preferred locale country: ${clientLocale.displayCountry}
<br />
Preferred locale language: ${clientLocale.displayLanguage}
<h3>Lower priority locales...</h3>
<c:forEach var="loc" items="${clientLocales}" begin="1">
    Locale: ${loc.displayName}
    <br />
     Locale country: ${loc.displayCountry}
   <br />
    Locale language: ${loc.displayLanguage}
  <br /><br />
</c:forEach>
</body>
</html>





Locale info

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
<head><title><fmt:message key="Welcome" bundle="i18n.WelcomeBundle" /></title></head>
<body>
<h2>Here is your Locale info...</h2>
<fmt:message key="Welcome" bundle="i18n.WelcomeBundle" />
<%--  <c:set var="clientLocale" value="${pageContext.request.locale}" />
Locale: <c:out value="${clientLocale.displayName}" />
 <br />
Locale country: <c:out value="${clientLocale.displayCountry}" />
<br />
Locale language: <c:out value="${clientLocale.displayLanguage}" />--%>
</body>
</html>