Java/JSP/I18N

Материал из Java эксперт
Перейти к: навигация, поиск

Current Locale

   <source lang="java">

<%@ 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>

<fmt:message key="Hello" /> <fmt:message key="and" /> <fmt:message key="Welcome" />

Locale: <c:out value="${pageContext.request.locale.language}" />_<c:out value="${pageContext.request.locale.country}" />

<fmt:formatNumber value="1000000" type="currency" />

</body> </html>


      </source>
   
  
 
  



Internationalized Web Applications: JavaServer Pages

   <source lang="java">

/* 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);
 }

}


      </source>
   
  
 
  



JSP: Define the string in tag (I18N)

   <source lang="java">

/* 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>

Book Information


The name of this book is <wroxtags:bookTitle/>.

It is published by <wroxtags:publisher/>.

The server used in all the examples is <wroxtags:containerName/>.

   </body>

</html>


      </source>
   
  
 
  



JSP Internationalization and Localized Content 1

   <source lang="java">

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

  • /


      </source>
   
  
 
  



JSP Internationalization and Localized Content 2

   <source lang="java">

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

  • /


      </source>
   
  
 
  



JSP Internationalization and Localized Content:Currency Formatting and locales

   <source lang="java">

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

  • /


      </source>
   
  
 
  



JSP Internationalization and Localized Content: Date Formatting and locale

   <source lang="java">

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

  • /


      </source>
   
  
 
  



Locale Display in a JSP

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jsp/jstl/core" prefix="c" %> <html> <head><title>Locale Display</title></head> <body>

Here is your preferred locale info...

<c:set var="clientLocale" value="${pageContext.request.locale}" /> <c:set var="clientLocales" value="${pageContext.request.locales}" /> Preferred locale: ${clientLocale.displayName}


Preferred locale country: ${clientLocale.displayCountry}
Preferred locale language: ${clientLocale.displayLanguage}

Lower priority locales...

<c:forEach var="loc" items="${clientLocales}" begin="1">

   Locale: ${loc.displayName}
   
Locale country: ${loc.displayCountry}
Locale language: ${loc.displayLanguage}

</c:forEach> </body> </html>


      </source>
   
  
 
  



Locale info

   <source lang="java">

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

Here is your Locale info...

<fmt:message key="Welcome" bundle="i18n.WelcomeBundle" /> <%-- <c:set var="clientLocale" value="${pageContext.request.locale}" /> Locale: <c:out value="${clientLocale.displayName}" />


Locale country: <c:out value="${clientLocale.displayCountry}" />
Locale language: <c:out value="${clientLocale.displayLanguage}" />--%> </body> </html>


      </source>