Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 12 Localization

Why does the program print the above output? Note that there is no corresponding property file for the fr_CA locale in the list of property files. So, the search continues to check the property files for the default locale. In this case, the default locale is en_US, and there is a property file for the en (English) locale. So, from the candidate locales, Java resolves to load the property file ResourceBundle_en.properties correctly.

Here is the final example. Replace the code in the main() method with this statement:

loadResourceBundle("ResBundl", Locale.getDefault());

The program prints the following:

Candidate locales for base bundle name ResBundl and locale English (United States) en_US

en

The exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name ResBundl, locale en_US

[... thrown stack trace elided ...]

You don’t have any resource bundle named ResBundl and you’ve given the default locale (en_US in this case). Java searches for the bundle for this locale, and you know that you have not provided any bundle with name ResBundl. So, the program crashes after throwing a MissingResourceException.

Formatting for Local Culture

Text is obviously the main aspect to be localized. However, there are many aspects that are handled differently based on the locale: date and time, numbers, and currencies. We’ll discuss each of these topics in detail now.

For localize text, the main approach you need to follow is not to hardcode the strings. The key idea to remember for date, time, currency, and numbers is to use culture-aware formatting to localize them. Figure 12-2 shows how the two important classes we’ll discuss in this section—NumberFormat and DateFormat—are both inherited from the Format base class; these classes are part of the java.text package and are useful for making locale-aware software.

 

Abstract base class

 

supporting formatting of

 

locale-sensitive

Format

information such as date,

time, and currency values

(abstract class)

 

 

NumberFormat

DateFormat

Base class for processing date

and time based on the locale

Base class for processing

numbers for percentage, SimpleDateFormat currency, etc.

Allows you to define a custom pattern for processing date-type

Figure 12-2.  The Format class and its important derived classes

376

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]