Senin, 28 Januari 2013

Internationalizing a GUI Form

Internationalizing a GUI Form

The following tutorial takes you through some of the basic steps of internationalization in NetBeans IDE. We will set up internationalization for one form and later on design that form. Then we internationalize the whole project, which contains several forms in a few different packages. You can internationalize applications either by specifying automatic internationalization or by using a special wizard.

Internationalizing a GUI Form at Design Time

In this exercise we will open the demo Java application project, which contains a well-known find dialog created using the GUI Builder. Next, we will switch on automatic internationalization for Form FindDialog.java. In order to test our internationalized GUI form, we will add a new locale to the properties file and run the form in the non-default locale.

Opening the Example Project

  1. Download and unzip the InternationalizeDemo.zip project to any location on your computer.
  2. Choose File > Open Project (Ctrl-Shift-O), navigate to the InternationalizeDemo project that you extracted in the last step, and click Open. The project folder might be in a containing folder that is also called InternationalizeDemo.
  3. Expand Source Packages > Demo and double-click FindDialog.java. The sample form opens in the GUI Builder.
    Find Dialog
    To view a demonstration of how to create this GUI form, click View Demo.

Switch Automatic Internationalization On

  1. Select the root node in the Navigator Window (named Form FindDialog).
  2. In the Properties window, select the checkbox in the Automatic Internationalization property.
  3. Click Upgrade in the GUI Form Format Upgrade dialog box. If the checkbox is selected, the IDE creates the Bundle.properties file in the demo package as it is set in the Properties Bundle File property. If you need to have this file in a different location, you can click the ellipsis (...) button and choose a location or directly type the path in the property's text field.
  4. In the Projects window, double-click the Bundle.properties node in the Projects Window or right-click the node and choose Edit. The properties file is opened in the Source Editor. As you can see, all appropriate Keys and Values for Form FindDialog.java are generated. (The name of each key is derived from the form file name and the component Variable name. For example, the key FindDialog.jLabel1.text is generated for a component with the variable name jLabel1 placed in form file FindDialog. The value jLabel1 represents component's Text property in this example.
  5. Close the Bundle.properties file.

Internationalizing Individual GUI Components

We will now use the GUI builder to enter internationalized strings for the JLabels and JButtons in the form.
  1. Select the appropriate GUI component (e.g. jLabel1) in the Design Area.
  2. In the Properties window, click the ellipsis (...) button for the Text property.
    Note: You can also internationalize other properties that have String values, such as Mnemonic, Accessible Name, Accessible Descriptor, and ToolTip.
  3. The property editor is switched to resource bundle mode. Check that the Bundle Name field is set to demo.Bundle and the Key field contains the string FindDialog.jLabel1.text
  4. Type Find What: in the Value field.
  5. Click OK.
Repeat the previous steps for all the components so that the form looks like the following picture:
internationalized form
Note: Steps 1-5 can be done in a simpler and faster way: just double click jLabel1 in the design view, change the text from jLabel1 to Find What:, and press Enter. The result is the same as from the steps given above.
To view a demonstration of internationalizing jLabel1, click View Demo
To make the components the same width, complete the steps below:
  1. Control-click all eight of the jCheckBoxes in the form to select them.
  2. With the jCheckBoxes selected, right-click any one of them and choose Set Same Size > Same Width from the pop-up menu.
  3. Apply Steps 1-2 to the three jButtons.

Adding a New Locale

  1. Select the root node in the Navigator Window (the Form FindDialog node).
  2. In the Properties window, click the ellipsis (...) button for the Design Locale property
  3. Choose es_ES from the Predefined Locales: combobox.
  4. Click OK. The new locale appears below the Bundle.properties node, as shown below:
    Properties file with multiple locales
  5. In the Projects window, right-click Bundle.properties and choose Open.
  6. Translate individual messages in the correspondent column of the table to a new language (for example, Spanish), as shown below:
  7. Press Ctrl-S to save your edits.
  8. Select the FindDialog.java tab to display the form you are internationalizing.
  9. Right-click the root node in the Inspector Window and choose Reload Form (alternatively, press Ctrl-R).
  10. Click Save in the Question dialog box that displays.
    The form is reopened and the Spanish locale is loaded in in the design, as shown below:

Testing a Non-Default Locale

  1. In the Projects window, right-click the InternationalizeDemo project and choose Properties.
  2. In the Categories pane, select the Run node.
  3. Enter -Duser.language=es -Duser.country=ES in the VM Options field.
  4. Click OK.
  5. Right-click the InternationalizeDemo project and choose Run. The IDE runs the FindDialog dialog box in the Spanish locale like shown below.



Tidak ada komentar:

Posting Komentar