Java/Servlets/I18N

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

Another Servlet Localization: Currency

import java.text.NumberFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CurrLocaleServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    //Get the client"s Locale
    Locale locale = request.getLocale();
    ResourceBundle bundle = ResourceBundle.getBundle("i18n.WelcomeBundle",
        locale);
    String welcome = bundle.getString("Welcome");
    NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
    String formatted = nft.format(1000000);
    //Display the locale
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html><head><title>" + welcome + "</title></head><body>");
    out.println("<h2>" + bundle.getString("Hello") + " "
        + bundle.getString("and") + " " + welcome + "</h2>");
    out.println("Locale: ");
    out.println(locale.getLanguage() + "_" + locale.getCountry());
    out.println("<br /><br />");
    out.println(formatted);
    out.println("</body></html>");
    out.close();
  } //end doGet
}





Hello world in Japanese

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloJapan extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    res.setContentType("text/plain; charset=Shift_JIS");
    PrintWriter out = res.getWriter();
    res.setHeader("Content-Language", "ja");
    Locale locale = new Locale("ja", "");
    DateFormat full = DateFormat.getDateTimeInstance(DateFormat.LONG,
                                                     DateFormat.LONG,
                                                     locale);
    out.println("In Japanese:");
    out.println("\u4eca\u65e5\u306f\u4e16\u754c");  // Hello World
    out.println(full.format(new Date()));
  }
}





Internationalization I18n

import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WelcomeServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    //Get the client"s Locale
    Locale locale = request.getLocale();
    ResourceBundle bundle = ResourceBundle.getBundle("i18n.WelcomeBundle",
        locale);
    String welcome = bundle.getString("Welcome");
    //Display the locale
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html><head><title>" + welcome + "</title></head><body>");
    out.println("<h2>" + welcome + "</h2>");
    out.println("Locale: ");
    out.println(locale.getLanguage() + "_" + locale.getCountry());
    out.println("</body></html>");
    out.close();
  } //end doGet
  // doPost method ...
}//WelcomeServlet





Servlet Localization

import java.text.NumberFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PerLocaleServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    //Get the client"s Locale
    Locale locale = request.getLocale();
    ResourceBundle bundle = ResourceBundle.getBundle("i18n.WelcomeBundle",
        locale);
    String welcome = bundle.getString("Welcome");
    NumberFormat nft = NumberFormat.getPercentInstance(locale);
    String formatted = nft.format(0.51);
    //Display the locale
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html><head><title>" + welcome + "</title></head><body>");
    out.println("<h2>" + bundle.getString("Hello") + " "
        + bundle.getString("and") + " " + welcome + "</h2>");
    out.println("Locale: ");
    out.println(locale.getLanguage() + "_" + locale.getCountry());
    out.println("<br /><br />");
    out.println("NumberFormat.getPercentInstance(): " + formatted);
    out.println("</body></html>");
  } //end doGet
}





Servlet Localization: Date

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.ListResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DateLocaleServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    //Get the client"s Locale
    Locale locale = request.getLocale();
    ResourceBundle bundle = ResourceBundle.getBundle("i18n.WelcomeBundle",
        locale);
    String welcome = bundle.getString("Welcome");
    String date = DateFormat.getDateTimeInstance(DateFormat.FULL,
        DateFormat.SHORT, locale).format(new Date());
    //Display the locale
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html><head><title>" + welcome + "</title></head><body>");
    out.println("<h2>" + bundle.getString("Hello") + " "
        + bundle.getString("and") + " " + welcome + "</h2>");
    out.println(date + "<br /><br />");
    java.util.Enumeration e = bundle.getKeys();
    while (e.hasMoreElements()) {
      out.println((String) e.nextElement());
      out.println("<br /><br />");
    }
    out.println("Locale: ");
    out.println(locale.getLanguage() + "_" + locale.getCountry());
    out.println("</body></html>");
  } //end doGet
}
// file: WelcomeBundle_es_ES.java
// import java.util.ListResourceBundle;
class WelcomeBundle_es_ES extends ListResourceBundle {
  static final Object[][] contents = {
  { "Welcome", "Huan Ying" } };
  public Object[][] getContents() {
    return contents;
  }
}





Servlet localization display

import java.util.Enumeration;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LocaleDisplay extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    //Get the client"s Locales
    Enumeration e = request.getLocales();
    //Get the preferred Locale
    Locale preferred = request.getLocale();
    String prefDisplay = "";
    if (preferred != null)
      prefDisplay = preferred.getDisplayName();
    //Display the preferred and any other locales
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html><head><title>Locale Display</title></head><body>");
    out.println("<h2>Here is your Locale info...</h2>");
    out.println("<b>Preferred Locale:</b> ");
    out.println(prefDisplay);
    out.println("<br />");
    out.println("Locale country: ");
    if (preferred != null)
      out.println(preferred.getDisplayCountry());
    out.println("<br />");
    out.println("Locale language: ");
    if (preferred != null)
      out.println(preferred.getDisplayLanguage());
    out.println("<br /><br />");
    out.println("<h3>Lower priority Locales...</h3>");
    Locale loc = null;
    while (e.hasMoreElements()) {
      loc = (Locale) e.nextElement();
      if (!(loc.getDisplayName().equals(prefDisplay))) {
        out.println("Locale: ");
        out.println(loc.getDisplayName());
        out.println("<br />");
        out.println("Locale country: ");
        out.println(loc.getDisplayCountry());
        out.println("<br />");
        out.println("Locale language: ");
        out.println(loc.getDisplayLanguage());
        out.println("<br /><br />");
      }//if
    }//while
    out.println("</body></html>");
  } //end doGet
}





Set I18N Response Header

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloSpain extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
      IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    res.setHeader("Content-Language", "es");
    Locale locale = new Locale("es", "");
    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
    fmt.setTimeZone(TimeZone.getDefault());
    out.println("En Espa\u00f1ol:");
    out.println("\u00a1Hola Mundo!");
    out.println(fmt.format(new Date()));
  }
}