JavaSercer Pages Chapter 11. Internationalization Taking the term
JavaSercer Pages Chapter 11. Internationalization Taking the term World Wide Web literally means that your web site needs to respect the languages and customs of all visitors, no matter where they come from. More and more, large web sites provide content in several different languages. Just look at a site like Yahoo!, which provides directory services in the local languages of more than 20 countries in Europe, Asia, and North America. Other good examples are CNN, with local news for 8 different countries, and Vitaminic (http://www.vitaminic.com), a site with MP3 music and artist information customized for different countries. If the site contains only static content, it’s fairly easy to support multiple languages: just make a static version of the site for each language. But this approach is not practical for a site with dynamic content. If you develop a separate site for each language, you will have to duplicate the code that generates the dynamic content as well, leading to maintenance problems when errors are discovered or when it’s time to add new features. Luckily, Java and JSP provide a number of tools to make it easier to develop one version of a site that can handle multiple languages. The process of developing an application that caters to the needs of users from different parts of the world includes two phases: internationalization and localization. Internationalization means preparing the application by identifying everything that will be different for different geographical regions, and providing means to use different versions of all these items instead of hardcoded values. Examples of this are labels and messages, online help texts, graphics, format of dates, times, and numbers, currencies, measurements, and sometimes even the page layouts and colors. You should note that instead of spelling out the word internationalization, the abbreviation I18N is often used. It stands for “an I followed by 18 characters and an N.” When an application has been internationalized, it can also be localized for different regions. This means providing the messages, the help texts, the graphics and so forth, as well as the rules for formatting dates, times, and numbers, for one or more regions that the internationalized application can use. Localization is sometimes abbreviated L10N, following the same logic as the I18N abbreviation. The set of localized information for one region is called a locale. Support for new locales can be added without changing the application itself. In this chapter, we first look at the basic Java classes used for internationalization. If you’re not a programmer, you can skim through this section without worrying about the details. (However, you should understand the terminology, and knowing a bit about the inner workings of these classes also makes it easier to understand the rest of the chapter.) We then develop a web application in which visitors can answer a poll question and see statistics over how other visitors have answered, using a set of custom actions that hide the Java classes to make internationalization a lot easier. You can reuse these custom actions in your own application to handle most internationalization needs. The poll site is localized for three languages. The initial language is based on the user’s browser configuration. The user can also explicitly select one of the supported languages. 11.1 How Java Supports Internationalization and Localization Java was designed with internationalization in mind and includes a number of classes to make the effort as painless as possible. The primary class used for internationalization represents a specific geographical region. Instances of this class are used by other classes to format dates and numbers, as well as including localized strings and other objects in an application. There are also classes for dealing with different character encodings, which we will see later in the chapter. 11.1.1 The Locale Class All Java classes that provide localization support use a class named java.util.Locale. An instance of this class represents a particular geographical, political, or cultural region, as specified by a combination of a language code and a country code. Java classes that perform tasks that differ depending on a user’s language and local customs, called locale-sensitive operations, use a Locale instance to decide how to operate. Examples of locale-sensitive operations are the interpretation of date strings and formatting numeric values. You create a Locale instance using a constructor that takes the country code and language code as arguments: java.util.Locale usLocale = new Locale(”en”, “US”); page 148
Note: If you are looking for inexpensive but high quality provider to host and run your serlvet application check Astra servlet hosting services