7.2 I18N and L10NInternationalization, often abbreviated as Overview" on page 250.Localization, often abbreviated as L10N, is the process of adapting an internationalized application to support a specific locale. For the most part, with JSTL, that means creating a set of resource bundles for specific locales.To develop internationalized Web applications that you can subsequently localize, you must have a basic understanding of locales, resource bundles, Unicode, and charsets, all of which are discussed in the following sections. LocalesThe most basic localization task is identifying geographical, political, or cultural regions, known as locales. Locale constants for countries and languages are defined by the International Standards Organization (ISO). Table 7.1 lists some examples of locale constants for selected countries.
You can also specify a variant for a locale. Variants are vendor- and browser-specific. For example, you can specify the variants WIN for Microsoft Windows or MAC for Macintosh. You can specify a locale variant with the <fmt:setLocale> action's variant attribute; for example, you could specify the Macintosh variant for France French like this: <fmt:setLocale value='fr-FR' variant='MAC'/>. In practice, locale variants are rarely used. Resource BundlesAs we discussed in "Overview" on page 250, a resource bundle is a collection of key/value pairs. You can specify resource bundles with a properties file or with a Java class.Properties files are by far the most popular way to specify resource bundles, even though a resource bundle specified as a Java class is more flexible than one specified as a properties file. The reasons for that popularity are simpleproperties files are easier to create than Java classes, and they do not need to be compiled.Resource bundles, whether they are specified with a properties file or implemented as a Java class, must reside in either the WEB-INF/classes directory or a subdirectory of WEB-INF/classes. Resource Bundles as Properties FilesListing 7.7 lists a simple properties file that specifies localized messages in English for a login page. Listing 7.7 A Properties File That Represents a Resource Bundle
In a properties file, lines beginning with the # character are comments. Key/value pairs are specified with this syntax: key=value. In a properties file, both keys and values are always strings. Resource Bundles as Java ClassesListing 7.8 lists a Java class that specifies a resource bundle equivalent to the one represented by the properties file listed in Listing 7.7.The Java class in the preceding listing extends java.util.ListResource Bundle, which defines one abstract method: getContents. That method returns a two-dimensional array of objects containing key/value pairs.Specifying a resource bundle with a Java class is more flexible than using a properties file because values are not limited to strings, as is the case for properties files. Also, if you specify a resource bundle with a Java class, you can calculate values, which you cannot do in a properties file. In practice, those features are rarely used, and as a result properties files are the preferred method of specifying resource bundles. Unicode and CharsetsInternally, the Java programming language uses Unicode to store characters. Unicode is a character coding system that assigns unique numbers for every character for every major language in the world. Listing 7.8 A Java Class That Represents a Resource Bundle
Java's use of Unicode means that JSP pages can store and display strings containing all characters found in all of the commonly used written languages. It also means that you can use Unicode escape sequences to represent characters that you may not find on your keyboard; Table 7.3 lists some of those characters.
|