IGreet.java
Top Next Download this source file


// IGreet.java - an Internationalized greet program.
// Shows how to use ListResourceBundle classes to customize strings.  Note that
// ListResourceBundles can contain any sort of Java object, not just String objects.
// (e.g., Images or arrays of values).  (See also IGreetMsgs*.java classes.)
//
// This program is both stand-alone and an Applet.  To run as a stand-alone program,
// double-click the jar file or run from the cmd line: java -jar IGreet.jar.
// (The details of creating such jar files are described elsewhere.)  The important
// part of this demo is the bottom of the displayMsgs method; the rest of the code
// merely supports a fancy demo and can be ignored.
//
// Written January 24, 2001 by Wayne Pollock

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class IGreet extends Applet
{
   private String [] args;
   private TextArea ta;

   public static void main ( String [] args )
   {
      Frame frame = new Frame( "IGreet: I18N ListResourceBundle Demo" );
      frame.setSize( 440, 180 );

      //Center the Frame:
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension frameSize = frame.getSize();
      int x = ( screenSize.width - frameSize.width ) / 2;
      int y = ( screenSize.height - frameSize.height ) / 2;
      frame.setLocation( x, y );
      frame.setLayout( new BorderLayout() );

      frame.addWindowListener( new WindowAdapter ()  // Window-closing code.
          {   public void windowClosing ( WindowEvent e )
              {  System.exit( 0 ); }
          }
      );

      IGreet ig = new IGreet();
      ig.args = args;
      frame.add( ig, "Center" );
      frame.setVisible( true );
      ig.init();
      ig.start();
   }

   public void init ()
   {
      setLayout( new BorderLayout() );
      ta = new TextArea( "", 5, 65, TextArea.SCROLLBARS_NONE );
        ta.setBackground( Color.white );
        ta.setEditable( false );
        add( ta, "South" );
      Label lbl = new Label( "IGreet - I18N Demo with ListResourceBundles", Label.CENTER );
        lbl.setFont( new Font( "SanSerif", Font.BOLD, 18 ) );
        add( lbl, "North" );

      if ( args == null )  // Then IGreet was run as an Applet, so fetch PARAMs:
      {
         String l = getParameter( "Language" );
         String c = getParameter( "Country" );

         if ( c == null )
         {  args = new String[1];
            args[0] = l;
         } else
         {  args = new String[2];
            args[0] = l;
            args[1] = c;
         }
      }

      displayMsg();  // Display internationalized text using locale info in this.args.
      validate();
   }

   private void displayMsg ()
   {
      Locale loc = Locale.getDefault();
      String lang, country;

      if ( args.length == 2 )
      {
         lang = args[0].toLowerCase();
         country = args[1].toUpperCase();
         loc = new Locale( lang, country );
      }
      else if ( args.length == 1 && 
                ( args[0].toLowerCase().equals("fr") ||
                  args[0].toLowerCase().equals("en")
                )
              )
      {
         lang = args[0].toLowerCase();
         country = "";
         loc = new Locale( lang, country );
      }
      else if ( args.length != 0 )
      {
         ta.append( "Usage: java -jar IGreet.jar <language code>"
          + " [ <country code> ]\n" );
         ta.append( "\n\tSupported locales: fr_FR, "
          + "en_US (default)" );
         return;
      }

      // You can get interesting info from a locale object:

      lang = loc.getDisplayLanguage();
      if ( lang.length() == 0 )  lang = "(none)";
      country = loc.getDisplayCountry();
      if ( country.length() == 0 )  country = "(none)";
      ta.append( "Locale: " + loc + "\n" );
      ta.append( "Language: " + lang + ", Country: " + country + "\n" );

      // The real work of this demo starts here:

      ResourceBundle msgs = ResourceBundle.getBundle( "IGreetMsgs", loc );

      ta.append( msgs.getString( "greet" ) + "\n" );
      ta.append( msgs.getString( "bye" ) + "\n" );

      // You can get other Objects too (such as an Integer) with msgs.getObject().
   }
}

IGreetMsgs.java
Top Next Download this source file


// The Base Properties class for IGreet.java
// This is the en_US (well, almost) default
// (ie this is used for unknown locales).
// Written by Wayne Pollock, Tampa Florida USA, 1/2001.

import java.util.*;

public class IGreetMsgs extends ListResourceBundle
{
    public Object [] [] getContents ()
    {
        return contents;
    }

    private Object [] [] contents =
    {
        { "greet", "Howdy Y'all!!" },
        { "bye", "Y'all come back now, Y'hear?" },
    };
}

IGreetMsgs_fr.java
Top Next Download this source file


// The French Properties class for IGreet.java
// (Must be provided since using fr_FR locale, even if empty):
// Written by Wayne Pollock, Tampa Florida USA, 1/2001.

import java.util.*;

public class IGreetMsgs_fr extends ListResourceBundle
{
    public Object [] [] getContents ()
    {
        return contents;
    }

    private Object  [] [] contents =
    {
        { "greet", "Bonjour!" },
        { "bye", "Au revoir!" },
    };
}

IGreetMsgs_fr_FR.java
Top Next Download this source file


// The France-French Properties class for IGreet.java
// This file over-rides "greet" but uses the IGreetMsgs.fr
// resourceBundle for "bye".
//
// Written by Wayne Pollock, Tampa Florida USA, 1/2001.

import java.util.*;

public class IGreetMsgs_fr_FR extends ListResourceBundle
{
    public Object [] [] getContents ()
    {
        return contents;
    }

    private Object [] [] contents =
    {
        { "greet", "'ello!" },
    };
}

(Manifest file used to create IGreet.jar.)
IGreet.mf
Top   Download this source file


Main-Class: IGreet
Implementation-Version: 1.1
Implementation-Vendor: Wayne Pollock, Tampa Florida USA
Implementation-URL: http://www.wpollock.com/AJava/IGreet/IGreet.jar




Send comments and mail to the WebMaster.