An Example:

   

An Example: InternationalizationTest

To better understand how all this fits together, take a look at this simple Java application that makes use of several of the features discussed in this chapter.

The simple application takes up to three command-line parameters that specify a locale and uses this information to

  • Display some information about the default locale and the one entered.

  • Try to load a ResourceBundle corresponding to the specified locale and print out what the Bundle contains.

  • Display the date, localized to the specified locale.

Besides the main application class called InternationalizationTest.java, the program requires three other files. The three files are .properties files that correspond to three supported locales. They will get converted into PropertyResourceBundles automatically by the virtual machine when they are loaded. The property files must be in the CLASSPATH for them to be found and loaded properly by the ClassLoader. As long as the ResourceBundles and .properties files are available via CLASSPATH, you don't need a separate ClassLoader to load them. If you were making an applet, on the other hand, you would need a ClassLoader to load the classes across the Internet. You can use the same ClassLoader instance that loaded the applet like this:

 ClassLoader loader = this.getClass().getClassLoader(); 

Listing 24.6 provides the complete listing of InternationalizationTest.

Listing 24.6 InternationalizationTest.java
 import java.util.*; import java.lang.*; import java.text.DateFormat; class InternationalizationTest extends Object {   public static void main(String args[])   {     // Declare variables to hold the three parameters     String lang = "";     String country = "";     String variant = "";     // Make sure the user passes the language variable on the command line     if ( args.length == 0 )     {       System.out.println("You must specify at least one parameter");       System.exit(1);     }     int argsSize = args.length;     // There was at least the language passed in     lang = args[0];     // Need to check the other two to see if they were passed in     if ( argsSize > 1 )       country = args[1];     if ( argsSize > 2 )       variant = args[2];     // Get the locale for the specific locale variables     Locale locale = new Locale( lang, country, variant );     // Get the Default Locale     Locale defaultLocale = Locale.getDefault();     // Print out the Locale information to the console     System.out.println(  "Default Locale is: "+ defaultLocale.getDisplayName() );     System.out.println("You have selected Locale: "+ locale.getDisplayName() );     System.out.println("Default language, localized for your locale is: " +             defaultLocale.getDisplayLanguage( locale ) );     System.out.println( "Default country name, localized: " + defaultLocale.getDisplayCountry( locale ) );     // Get a resource bundle based on the passed in locale     ResourceBundle bundle = null;     try     {       bundle = ResourceBundle.getBundle( "TestBundle", locale );       System.out.println( "Resources available are: ");       System.out.println("OK Label: " + bundle.getString("OK_LABEL") );       System.out.println("Cancel Label: " + bundle.getString("CANCEL_LABEL") );     }     catch( MissingResourceException e)     {       System.out.println( "No resources available for that locale." );     }     // Display date and time information based on the locale instance     DateFormat myFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);     Calendar myCalendar = Calendar.getInstance( locale );     System.out.println( "The localized date and time is: " +  myFormat.format( myCalendar.getTime() ) );    } } 

Listing 24.7 through 24.9 are the provided property files for the three supported locales. If you needed to support more locales, all you would have to do is provide a .properties file with the locale name and put the OK_LABEL and CANCEL_LABEL values in the file. This is why Java internationalization is so easy if it's done correctly. It takes little or no software changes to support new locales.

Note

You should always have a base resource bundle in case a locale that is not supported is passed in. In this example, the properties file called TestBundle.properties is the default case.


Troubleshooting Tip

If you are having trouble in your application when it's trying to locate the .properties file(s), see "Finding the .properties Files" in the "Troubleshooting" section at the end of the chapter.


Listing 24.7 TestBundle_en_US.properties
 OK_LABEL=OK CANCEL_LABEL=Cancel 
Listing 24.8 TestBundle_de_DE.properties
 OK_LABEL=Gut CANCEL_LABEL=Abbrechen 
Listing 24.9 TestBundle.properties
 OK_LABEL=Okay CANCEL_LABEL=Forget about it! 

Figures 24.1, 24.2, and 24.3 show output from the InternationalizationTest program.

Figure 24.1. Output from Listing 24.6 using the American English Locale.

graphics/24fig01.gif

Figure 24.2. Output from Listing 24.6 using the German Locale.

graphics/24fig02.gif

Figure 24.3. Output from Listing 24.6 using the Default English Locale.

graphics/24fig03.gif

Here's one final important note on adding internationalization to your Java applications. As with most everything else in designing software applications, the most important time to think about internationalization for your Java applications in before you start coding your applications. The cost to design and programming is always much less earlier in the process. If you think that your application might have to support other locales, go ahead and plan to use the Java internationalization features early so you don't find yourself ripping your program apart later to add it in.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net