|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 6
In Java, the focal point of all internationalization activity is the java.util.Locale class, which serves to encapsulate the country code, language code, and the optional variant code. The Locale class also provides a number of convenient constants representing the most commonly used locales. Consider the following
snippet, which establishes the locale for the Web application:
Locale locale=null;
if (lang.equals("German")) {
locale=Locale.GERMANY;
} else if (lang.equals("French")) {
locale=Locale.FRANCE;
} else if (lang.equals("Swedish")) {
locale=new Locale("sv","SE");
} else {
locale=Locale.US;
}
While the Locale instance is typically obtained from a predefined constant within the Locale class, it can also be instantiated using the class's constructor. You can do this by specifying the language code and country
code (there is also another version of the Locale constructor that additionally accepts an application-specific variant code). The language code is two lowercase letters,
and the country code is always two uppercase letters. (As mentioned earlier, you can find a link to lists of valid country
and language code in Resources.)
Since I do not have a predefined constant for Sweden, observe that its Locale instance is instantiated by explicitly passing the language and country code as:
locale=new Locale("sv","SE");
Once the locale has been established, you can retrieve its corresponding bundle:
ResourceBundle bundle =
ResourceBundle.getBundle("Message", locale);
You can store the bundles anywhere in the classpath, as understood by your application server or JSP engine. The getBundle() invocation initiates the search for the bundle after extracting the language code (xx, say) and the country code (YY, say) from the locale object passed as a parameter. (You can also invoke getBundle() without passing an instance of Locale, in which case the system default locale is used.) You conduct the search first by looking for the presence of Message_xx_YY.class within the classpath. If this is absent, the search tries to locate Message_xx.class and then Message.class. If there is no matching classfile, the search proceeds to locate a matching property file instead, starting with Message_xx_YY.properties, moving to Message_xx.properties, and finally, Message.properties. If the lookup fails without a match, getBundle() throws a MissingResourceException.
<%@ page import="java.util.*" %>
<%
String lang = request.getParameter("lang");
if (lang == null) {
%>
<html>
<head>
<title>
Music Without Borders
</title>
</head>
<body bgcolor="#33CCFF">
<font face="Times New Roman,Times" size=+3>
Music Without Borders
</font>
<hr>
<p>
Please select a language:
<form action="i18nDemo.jsp" method="post">
English <input type="radio" name="lang" value="English" checked>
Deutsch <input type="radio" name="lang" value="German">
Français <input type="radio" name="lang" value="French">
<p>
<input type="submit" value="Continue">
</form>
</body>
</html>
<%
} else {
Locale locale=null;
if (lang.equals("German")) {
locale=Locale.GERMANY;
} else if (lang.equals("French")) {
locale=Locale.FRANCE;
} else if (lang.equals("Swedish")) {
locale=new Locale("sv","SE");
} else {
locale=Locale.US;
}
session.putValue("myLocale",locale);
ResourceBundle bundle =
ResourceBundle.getBundle("Message",locale);
for (Enumeration e = bundle.getKeys();e.hasMoreElements();) {
String key = (String)e.nextElement();
String s = bundle.getString(key);
session.putValue(key,s);
}
%>
<jsp:forward page="eshop.jsp" />
<%
}
%>
After retrieving the bundle, you obtain the resources for the locale along with their associated values and place them into the session as:
for (Enumeration e = bundle.getKeys();e.hasMoreElements();) {
String key = (String)e.nextElement();
String s = bundle.getString(key);
session.putValue(key,s);
}
Once you have initialized the session with all the necessary localized information, the request is forwarded to eshop.jsp (shown in Listing 4), which facilitates the main view for the online store.
As every good JSP page should, eshop.jsp deals almost exclusively with presenting the application's UI to the client. In fact, the only processing it performs is
when it formats the price field according to the selected locale. Take a look at the method computePrice() declared within the page, which helps handle the display of currencies and formats them in a manner appropriate to the locale.
The real work is done by the java.text.NumberFormat object for the specified locale obtained from the getCurrencyInstance() invocation. When the format() method is invoked on this instance, it returns a String that includes the correctly formatted number, along with the appropriate currency sign. Observe that I also make use of the
resource dollar.exchRate to convert the input dollar amount to the equivalent currency of the locale. Otherwise, the CDs would be a major bargain
in Sweden, for instance, where a dollar is worth about 8.40 Swedish Krona!
Server-side Java: Read the whole series -archived on JavaWorld