Servlet Localization : I18N « Servlets « Java






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

}

           
       








Related examples in the same category

1.Internationalization I18n
2.Another Servlet Localization: Currency
3.Servlet Localization: Date
4.Servlet localization display
5.Set I18N Response Header
6.Hello world in Japanese