Recipe 15.1 Creating a Button with I18N Resources


Problem

You want your program to take "sensitivity lessons" so that it can communicate well internationally.

Solution

Your program must obtain all control and message strings via the internationalization software. Here's how:

  1. Get a ResourceBundle.

    ResourceBundle rb = ResourceBundle.getBundle("Menus");

    I'll talk about ResourceBundle in Recipe 15.6, but briefly, a ResourceBundle represents a collection of name-value pairs (resources). The names are names you assign to each GUI control or other user interface text, and the values are the text to assign to each control in a given language.

  2. Use this ResourceBundle to fetch the localized version of each control name.

    Old way:

    somePanel.add(new JButton("Exit"));

    New way:

    try { label = rb.getString("exit.label"); } catch (MissingResourceException e) { label="Exit"; } // fallback somePanel.add(new JButton(label));

    This is quite a bit of code for one button but distributed over all the widgets (buttons, menus, etc.) in a program, it can be as little as one line with the use of convenience routines, which I'll show in Recipe 15.4.

What happens at runtime?

The default locale is used, since we didn't specify one. The default locale is platform-dependent:

  • Unix/POSIX: LANG environment variable (per user)

  • Windows: Control Panel Regional Settings

  • International

  • ResourceBundle.getBundle( ) locates a file with the named resource bundle name (Menus in the previous example), plus an underscore and the locale name (if any locale is set), plus another underscore and the locale variation (if any variation is set), plus the extension .properties . If a variation is set but the file can't be found, it falls back to just the country code. If that can't be found, it falls back to the original default. Table 15-1 shows some examples for various locales.

    Table 15-1. Property filenames for different locales

    Locale

    Filename

    Default locale

    Menus.Properties

    Swedish

    Menus_sv.properties

    Spanish

    Menus_es.properties

    French

    Menus_fr.properties

    French-Canadian

    Menus_fr_CA.properties


    Locale names are two-letter ISO language codes (lowercase); locale variations are two-letter ISO country codes (uppercase).

    Setting the locale

    On Windows, go into Regional Settings in the Control Panel. Changing this setting may entail a reboot, so exit any editor windows.

    On Unix, set your LANG environment variable. For example, a Korn shell user in Mexico might have this line in his or her .profile :

    export LANG=es_MX

    On either system, for testing a different locale, you need only define the locale in the System Properties at runtime using the command-line option -D, as in:

    java -Duser.language=es Browser

    to run the program named Browser in the Spanish locale.



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

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