Localizing Application Output

Remember the pages for setting user preferences from Chapter 9? One of the pages allows the user to select her preferred language; let’s add what’s needed for actually using the selected language. Figure 11-1 shows one of the preferences pages when Swedish is selected as the preferred language.

User information page with Swedish as the selected language
Figure 11-1. User information page with Swedish as the selected language

Java was designed with internationalization in mind and includes a number of classes to make the process as painless as possible. In i18n terminology, a locale represents a specific geographical region. In Java, a locale is represented by an instance of the java.util.Locale class. Java includes other classes for formatting dates and numbers according to the rules defined for a locale, and classes to help you include localized strings and other objects in an application.

You create a Locale instance using a constructor that takes a country code and language code as arguments:

java.util.Locale usLocale = new Locale("en", "US");

Here, a Locale for U.S. English is created. George Bernard Shaw (a famous Irish playwright) once observed, “England and America are two countries divided by a common language,” so it’s no surprise that both a language code and a country code are needed to describe some locales completely. The language code, a lowercase two-letter combination, is defined by the ISO 639 standard ...

Get JavaServer Faces now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.