// 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 "
+ " [ ]\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().
}
}
|