View comments | RSS feed

Localization with Java

Java was designed to support I18N and L10N and includes the following classes that help you localize your applications:

In addition, the servlet API includes methods that make the request and response objects localization-aware. The JSP Standard Tag Library (JSTL) includes custom tags that make it easier for you to develop localized JSPs.

This section describes the basics of the Locale object and the language and country codes that it uses. For more information on support for localization in J2EE, see "Resources".

Understanding locales

Instances of the java.util.Locale class are not used directly, but are instead used by other classes to provide regional information and language settings. One class that uses locale information is the DateFormat class, as the following example shows:

DateFormat df  = DateFormat.getDateInstance(DateFormat.LONG, new Locale("ja","JP"));
String date = df.format(new Date());

This example formats the date in the Japanese date format.

When instantiating a Locale object, you provide the combination of a language code with two lowercase characters, and country code with two uppercase characters, as the following example shows:

java.util.Locale locale = new Locale("ja","JP");

In this example, ja is the language code and JP is the country code. The reason that you specify a language code and a country code is that some countries have multiple official languages. For example, Switzerland has four official languages.

Viewing available locales

Java provides many methods for getting locale-related information, including the following:

The following code example lists the available locale information on the system that the JVM is currently running on:

<%@ page import="java.util.*, java.text.*" %>
<%@ page contentType="text/html; charset=UTF-8" %>
...
<% Locale[] availableLocales = Locale.getAvailableLocales(); %>
<TABLE>
<% for(int i=0; i < availableLocales.length; i++)  {%>
<tr>
��<td><%= availableLocales[i].getLanguage() %></td>
��<td><%= availableLocales[i].getCountry() %></td>
��<td><% DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, 
��  DateFormat.FULL, availableLocales[i]); %><%= df.format(new Date()) %></td>
��<td>  <% NumberFormat nf = NumberFormat.getNumberInstance(availableLocales[i]); %>
��  <%= nf.format(123456.78) %></td>
��<td><% nf = NumberFormat.getCurrencyInstance(availableLocales[i]); %>
��  <%= nf.format(1234567.89) %></td>
</tr>
<% } // end of for() loop %>
</TABLE>
...

Understanding language codes

Language codes represent a language with two lowercase letters, and are defined by the ISO 639 standard.

Common language codes include:

For a list of language codes, see http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt.

Understanding country codes

Country codes represent the country with two uppercase letters, and are defined by the ISO 3166 standard.

Common country codes include:

For a list of country codes, see http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html.

Comments


igor.muhin said on Oct 17, 2002 at 9:09 AM :
JRun is the best. The best server, the best documentation.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/jrun/4/Programmers_Guide/i10n3.htm