Set Language using Locale

Set Language using Locale


Posted in : Java Posted on : June 27, 2011 at 7:42 PM Comments : [ 0 ]

In this example we will discuss how can we set the language in java servlet.

Set Language using Locale

In this example we will discuss how can we set the language in java servlet.

In the example given below is discussing about how to set and get language code using Locale. Locale is an Internationalization concept which describes a particular language and geographical area. Locale object keeps only a little important members. These are :

* language-code : Language code specifies the code of language which is used for the language into which the customer wants to do work. The language code is set by ISO 639.

* region-code : The region code refers to the country code. These codes are defined by the ISO 3166.

* optional variant-code : The variant code are the code that are used for software vendors. This code specifies the additional functionality or customization given by the S/W vendors which is not possible with just a language code and region code.

In this example I have tried to show you that how could you set the Locale language and how it would be get. I am also trying to show you that how can java.util.Date be used with Locale.

Example :

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 SetLocale extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG, Locale.FRENCH);
dateFormat.setTimeZone(TimeZone.getDefault());
Locale.setDefault(Locale.FRENCH);
pw.println("The Locale French code is = "
+ Locale.getDefault());
pw.println("<br>And the Date format = "
+ dateFormat.format(new Date()));
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>locale</display-name>
<servlet>
<servlet-name>SetLocale</servlet-name>
<servlet-class>SetLocale</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SetLocale</servlet-name>
<url-pattern>/SetLocale</url-pattern>
</servlet-mapping>
</web-app>

Output :

When you will execute the above example you will find the following output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics