Skip to Main Content
Java Cookbook
book

Java Cookbook

by Ian F. Darwin
June 2001
Intermediate to advanced content levelIntermediate to advanced
888 pages
21h 1m
English
O'Reilly Media, Inc.
Content preview from Java Cookbook

Writing Internationalization Convenience Routines

Problem

You want convenience.

Solution

I’ve got it.

Discussion

Convenience routines are mini-implementations that can be more convenient and effective than the general-purpose routines. Here I present the convenience routines to create buttons, menus, etc. First, a simple one, mkMenu( ) :

/** Convenience routine to make up a Menu with its name L10N'd */
Menu mkMenu(ResourceBundle b, String menuName) {
    String label;
    try { label = b.getString(menuName+".label"); }
    catch (MissingResourceException e) { label=menuName; }
    return new Menu(label);
}

There are many such routines that you might need; I have consolidated several of them into my class I18N.java , which is part of the com.darwinsys.util package. All methods are static, and can be used without having to instantiate an I18N object because they do not maintain any state across calls. The method mkButton( ) creates and returns a localized Button, and so on. The method mkDialog is slightly misnamed, since the JOptionPane method showMessageDialog() doesn’t create and return a Dialog object, but it seemed more consistent to write it as shown here:

package com.darwinsys.util; import java.util.*; import javax.swing.*; /** Set of convenience routines for internationalized code. * All convenience methods are static, for ease of use. */ public class I18N { /** Convenience routine to make a JButton */ public static JButton mkButton(ResourceBundle b, String name) { String label; try { label ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Practical Cloud-Native Java Development with MicroProfile

Practical Cloud-Native Java Development with MicroProfile

Emily Jiang, Andrew McCright, John Alcorn, David Chan, Alasdair Nottingham
Distributed Computing in Java 9

Distributed Computing in Java 9

Raja Malleswara Rao Malleswara Rao Pattamsetti

Publisher Resources

ISBN: 0596001703Supplemental ContentCatalog PageErrata