Isolating Locale-Specific Data |
Identifying the Locale-specific Objects
If your application has a user interface, it contains many locale-specific objects. To get started, you should go through your source code and look for objects that vary withLocale
. Your list may include the objects instantiated from the following classes:You'll notice that this list doesn't contain objects representing numbers, dates, times, or currencies. The format of these objects varies with
String
Component
Graphics
Image
Color
AudioClip
Locale
, but the objects themselves do not. For example, you format aDate
according toLocale
, but you use the sameDate
object regardless ofLocale
. Instead of isolating these objects in aResourceBundle
, you format them with special locale-sensitive formatting classes. You'll learn how to do this in the Dates and Times section of the Formatting lesson.In general, the objects stored in a
ResourceBundle
are pre-defined and ship with the product. These objects are not modified while the program is running. For instance, you should store aMenu
label in aResourceBundle
because it is locale-specific, and will not change during the program session. However, you should not isolate in aResourceBundle
aString
object entered by the end-user in aTextField
. Data such as thisString
may vary from day to day. It is specific to the program session, not to theLocale
in which the program runs.Usually, most of the objects you need to isolate in a
ResourceBundle
areString
objects. However, not allString
objects are locale-specific. For example, if aString
is a protocol element used by inter-process communication, it doesn't need to be localized because the end-users never see it. The decision whether to localize someString
objects is not always clear. Log files are a good example. If a log file is written by one program and read by another, then both programs are using the log file as a buffer for communication. Suppose end-users occasionally check the contents of this log file. Shouldn't the log file be localized? On the other hand, if the log file is rarely checked by end-users the cost of translation may not be worthwhile. Your decision to localize this log file depends on a number of factors: program design, ease of use, cost of translation, and supportablity.Organizing ResourceBundle Objects
You can organinze yourResourceBundle
objects by loading each one of them with a different category of objects. For example, you might want to load all of yourButton
labels into aResourceBundle
calledButtonLabelsBundle
. Loading the related objects into differentResourceBundle
objects offers several advantages:
- Your code is easier to read and maintain.
- Retrieving an object from a
ResourceBundle
is faster if theResourceBundle
contains a small number of objects.- Translators will find it easier to work with smaller properties files.
Most locale-specific data consists of
String
objects. If yourString
objects need to be translated, store them in aResourceBundle
object that is backed by properties files. If you use properties files, translators can add support for additional languages by creating new properties files. Your localizers won't have to call you up and ask you to create more classes every time a newLocale
requires support.
Isolating Locale-Specific Data |