Introduction |
If you take a look at the internationalized source code, you'll notice that the hardcoded English messages have been removed. Because the messages are no longer hardcoded, and because the language code is specified at run time, the same executable can be distributed worldwide. No recompilation is required for localization. The program has been internationalized.You may be wondering what happened to the text of the messages, or what the language and country codes mean. Don't worry. You'll learn about these concepts as you step through the process of internationalizing the sample program:
1. Create the Properties Files
A properties files stores information about the characteristics of a program or environment. A properties file is in plain text format. You can create it with just about any text editor.In the example, the properties files store the translatable text of the messages to be displayed. Before the program was internationalized, the English version of this text was hardcoded in the
System.out.println
statements. The default properties file, which is calledMessagesBundle.properties
, contains the following lines:Now that the messages are in a properties file, they can be translated into various languages. No changes to the source code are required. The French translator has created a properties file calledgreetings = Hello farewell = Goodbye inquiry = How are you?MessagesBundle_fr_FR.properties
, which contains these lines:Notice that the values to the right side of the equal sign have been translated, but that the keys on the left side have not been changed. These keys must not change, because they will be referenced when your program fetches the translated text.greetings = Bonjour. farewell = Au revoir. inquiry = Comment allez-vous?The name of the properties file is important. The name of the
MessagesBundle_fr_FR.properties
file contains thefr
language code and theFR
country code. These codes are also used when creating aLocale
object.2. Define the Locale
ALocale
object identifies a particular language and country. The following statement defines aLocale
for which the language is English and the country is the United States:The next example createsaLocale = new Locale("en","US");Locale
objects for the French language in Canada and in France:The program is flexible. Instead of using hardcoded language and country codes, the program gets them from the command line at run time:caLocale = new Locale("fr","CA"); frLocale = new Locale("fr","FR");String language = new String(args[0]); String country = new String(args[1]); currentLocale = new Locale(language, country);Locale
objects are only identifiers. After defining aLocale
, you pass it to other objects that perform useful tasks, such as formatting dates and numbers. These objects are called locale-sensitive, because their behavior varies according toLocale
. AResourceBundle
is an example of a locale-sensitive object.3. Create a ResourceBundle
ResourceBundle
objects contain locale-specific objects. You useResourceBundle
objects to isolate locale-sensitive data, such as translatable text. In the sample program, theResourceBundle
is backed by the properties files which contain the message text we want to display.The
ResourceBundle
is created as follows:The arguments passed to themessage = ResourceBundle.getBundle("MessagesBundle",currentLocale);getBundle
method identify which properties file will be accessed. The first argument,MessagesBundle
, refers to this family of properties files:TheMessagesBundle_en_US.properties MessagesBundle_fr_FR.properties MessagesBundle_de_DE.propertiesLocale
, which is the second argument ofgetBundle
, specifies which of theMessagesBundle
files is chosen. When theLocale
was created, the language code and country code were passed to its constructor. Note that the language and country codes followMessagesBundle
in the names of the property files.Now, all you have to do is get the translated messages from the
ResourceBundle
.4. Fetch the Text from the ResourceBundle
The properties files contain key/value pairs. The values consist of the translated text that the program will display. You specify the keys when fetching the translated messages from theResourceBundle
with thegetString
method. For example, to retrieve the message identified by the greetings key, you invokegetString
as follows:The sample program uses the keyString msg1 = messages.getString("greetings");greetings
because it reflects the content of the message, but it could have used some otherString
, such ass1
ormsg1
. Just remember that the key is hardcoded in the program, and it must be present in the properties files. If your translators accidentally modify the keys in the properties files,getString
won't be able to find the messages.Conclusion
That's it. As you can see, internationalizing a program isn't too difficult. It requires some planning, and a little extra coding, but the benefits are enormous. To provide you with an overview of the internationalization process, the sample program in this lesson was kept intentionally simple. As you read the lessons that follow, you'll learn about the more advanced internationalization features of the Java programming language.
Introduction |