HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Global Applications with JDK 1.1"
Vol. 2, Issue 5, p. 54

	

Listing 1: Making an Empty ResourceBundle.

New
	public class MyResources extends ListResourceBundle {
	// boilerplate
	public static ResourceBundle rb =
	ResourceBundle.getBundle("MyResources");
	public Object[][] getContents() { return contents; }
	static final Object[][] contents = {
	In ResourceBundle
	};
	}
	
Listing 1A: Move Strings to Resource Bundles

Old

	myCheckbox = new Checkbox("Clean ink cartridge before printing document");

	In ResourceBundle:
	...

New

	myCheckbox = new Checkbox(MyResources.rb.getString(
   "CleanCartridge"));

	// insert localized {key, value} pairs here
	{"CleanCartridge", "Clean ink cartridge before printing 
   document"},
	...

Listing 2: Translating Resource Bundles.

Old

	// insert localized {key, value} pairs here
	{"CleanCartridge", "Clean ink cartridge before printing 
   document"},

New

	In ResourceBundle:
	{"CleanCartridge", "Cleanez le cartridge de inque apr¸s que... "},

Listing 3: Moving Objects to Resource Bundles.

Old

	myCheckbox = new Checkbox("Clean ink cartridge before printing document");

	In ResourceBundle:
	...



New

	myCheckbox = (Checkbox) MyResources.rb.getObject(
  "CleanCartridge");

	// insert localized {key, value} pairs here
	{"CleanCartridge", new Checkbox("Clean ink cartridge before printing document")},
	...

Listing 4: MessageFormat instead of Concatenation.

Old

	myCheckbox = new Checkbox(MyResources.rb.getString(
   "DeleteBefore") + someDate);

	In ResourceBundle:
	{"DeleteBefore", "Delete all files before "},

New

	MessageFormat mf = new MessageFormat(MyResources.rb.getString("DeleteBefore"));
	myCheckbox = new Checkbox(mf.format(new Object[] {someDate}));

	In ResourceBundle:
	{"DeleteBefore", "Delete files before {0}"},

Note: The reason for using the array of objects for the parameter is to allow multiple arguments. 
There will probably be convenience methods in the future to make this a bit smoother.

Listing 5: One-Line MessageFormat.

New

	myCheckbox = new Checkbox(MessageFormat.format(
	MyResources.rb.getString("DeleteBefore"), new Object[] {some 
  Date}));

Listing 6: Number Output.

Old

	myTextField.setText(myNumber);

In ResourceBundle:
	...

New

	NumberFormat nf = (NumberFormat)(MyResources.rb.getObject("PageNumberFormat"));
	myTextField.setText(nf.format(myNumber));

In ResourceBundle:
	{"PageNumberFormat", new DecimalFormat("#,##0")},
	...

Listing 7: Number Output from String Resource.

Old

	myTextField.setText(myNumber);

In ResourceBundle:
	...

New

	NumberFormat nf = NumberFormat.getInstance();
	if (nf instanceof DecimalFormat) 
((DecimalFormat)nf).applyPattern(MyResources.rb.getString("PageNumberFormat"));
	myTextField.setText(nf.format(myNumber));

In ResourceBundle:
	{"PageNumberFormat", "#,##0"},
	...

Listing 8: Number Input.

Old

	try {
	myNumber = Integer.parseInt(myTextField.getText());
	} catch (NumberFormatException e) {
	alertBadNumber(e);
	}

New

	try {
	myNumber = nf.parse(myTextField2.getText());
	} catch (ParseException e) {
	alertBadNumber(e);
	}

Listing 9: String Comparison.

Old

	if (string1.equals(string2)) {...
	...
	if (string1.compare(string2) < 0) {...

New

	Collator col = Collator.getInstance();
	if (col.equals(string1, string2)) {...
	...
	if (col.compare(string1, string2) < 0) {...

Listing 10: Using CollationKey.

New

	// make up a list of sort keys
	CollationKey[] keys = new CollationKey[sourceStrings.length];
	for (int i = 0; i < sourceStrings.length; ++i) {
	keys[i] = col.getCollationKey(sourceStrings[i]);
	}
	// now sort and stuff them into an AWT List
	sort(keys);
	List list = new List();
	for (int i = 0; i < sourceStrings.length; ++i) {
	list.addItem(keys[i].getSourceString());
	}

Listing 11: Replacing Range Tests.

Old

	for (i = 0; i < string.length(); ++i) {
	char ch = string.charAt(i);
	if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
	// we have a letter, do something with it.

New

	for (i = 0; i < string.length(); ++i) {
	char ch = string.charAt(i);
	if (Character.isLetter(ch)) {
	// we have a letter (including non ASCII), do something with it.

Listing 12: Replacing Type Tests.

Old

	for (i = 0; i < string.length(); ++i) {
	char ch = string.charAt(i);
	if (ch == '(' || ch == '{' || ch == '[') {
	 // we have an open brace, do something with it.

New

	for (i = 0; i < string.length(); ++i) {
	char ch = string.charAt(i);
	if (Character.getType(ch) == Character.START_PUNCTUATION) {
	 // we have an open brace (including non ASCII), do something with it.

Listing 13: Going Word-by-Word.

	BreakIterator boundary = BreakIterator.getWordInstance();
	boundary.setText(stringToExamine);
	int start = boundary.first();
	for (int end = boundary.next(); 
	 end != BreakIterator.DONE; 
	 start = end, end = boundary.next()) {
	 System.out.println(source.substring(start,end));
	}

Listing 14: Testing Word Breaks.

	if (currentIndex < 0 || currentIndex > stringToExamine.length())
	 return false;
	if (currentIndex == 0 || currentIndex == stringToExamine.length())
	 return true;
	int discard = boundary.following(currentIndex);
	if (boundary.previous() == currentIndex) 
	 return true;
	return false;

Listing 15: Using Foreign Character Sets.

	// convert from ISO 8859-2 into Macintosh Central European
	String string = new String(foreignBytes[],"8859_2");
	otherBytes = string.getBytes("MacCentralEurope");

Listing 16: Multilingual Text Handling.

	NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);
	// or
	NumberFormat nf = NumberFormat.getInstance(new Locale("fr","",""));

Listing 17: Listing Locales.

	numberLocaleMenu = new Menu("&Locale");
	Locale[] locales = NumberFormat.getAvailableLocales();
  for (int i = 0; i < locales.length; ++i) {
	numberLocaleMenu.addItem(locales[i].getDisplayName());
	}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.