The following tutorial shows you ways how to internationalize GUI forms in
NetBeansTM IDE 5.0. In the first exercise, we will
use the GUI Builder's string editor to set the display text for a GUI component
with an internationalized string as you design the form. The second exercise
shows you how to use the Internationalization wizard to internationalize hard-coded
strings in multiple files at once. The last exercise shows you how to internationalize
strings in a single file and how to easily insert an internationalized string
in source code while you are coding.
Expected duration: 30 minutes
Prerequisites
This tutorial assumes you have some basic knowledge of, or programming experience
with, the following technologies:
NetBeans IDE
Software Needed for the Tutorial
Before you begin, you need to install the following software on your computer:
Internationalization permits applications to be adapted to
various languages and regions without requiring engineering changes or recompilation.
Internationalized programs enable textual elements, such as status messages
and GUI component labels, to be stored outside the source code and retrieved
dynamically rather than hard-coded in the program.
You typically store your internationalized strings in properties
files, in the form of key/value pairs. The key is the identifier used by the
program to retrieve the text, and the value is the actual text. You create one
properties file for each locale (or language) in which you translate the program.
The keys are the same in each locale - only the strings are different.
The IDE provides tools for doing each of the following:
Inserting internationalized strings as you create a GUI form or Java program
Replacing all hard-coded strings in an individual file or group of files
with internationalized strings
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 GUI Builder (Matisse). Next, we will
create a new properties file which will contain internationalized strings. Then,
we will use GUI Builder's string editor to change a text of components to an
internationalized strings. In order to test our internationalized GUI form,
we will add a new locale to properties file and run the form in the non-default
locale.
Choose File > Open Project (Ctrl-Shit-O), select the InternationalizeDemo
folder you created in the last step, and click Open.
Expand Source Packages > Demo and double-click FindDialog.java.
The sample form opens in the GUI Builder.
To view a demonstration of how to create this GUI form, click
View Demo.
Creating a Properties File
Choose File > New File or click the New File icon in the IDE toolbar.
Select the Properties File template from the Other category and click
Next.
Type Bundle in the File Name field and specify the folder
src/demo location.
Click Finish.
The IDE creates the Bundle.properties file (see the Projects
window) and opens the Bundle(default language) file in the
Source Editor.
Internationalizing Individual GUI Components
We will now use the GUI builder to enter internationalized
strings for the JLabels and JButtons in the form.
Select the appropriate GUI component (e.g. jLabel1) in the Design
Area.
In the Properties window, click ellipsis (...) button for the Text property.
Note that you can also internationalize other properties that have String
values, such as Mnemonic, Accessible Name, Accessible Descriptor, and ToolTip.
Select Resource Bundle in the Select Mode combo box. The property editor
switches to resource bundle mode.
For the Bundle Name field, click Browse to select the src/demo/Bundle.properties
file.
Type Find_What: in the Key field and Find What: in the
Value field.
Click OK.
Repeat the previous steps for all components so that the form looks like
the following picture:
To view a demonstration of internationalizing jLabel1 , click
View Demo
Adding a New Locale
Right-click Bundle.properties in the Projects window and choose
Add Locale.
Enter es in the Language Code field.
Enter ES in the Country Code field.
Click OK.
The new locale appears below the Bundle Properties node, as shown below:
In the Projects window, right-click Bundle.properties and choose
Open.
Translate individual messages in the correspondent column of table to the
new (e.g. Spanish) language, as shown below:
Testing a Non-Default Locale
In the Projects window, right-click the InternationalizeDemo project and
choose Properties.
In the Categories pane, select the Run node.
Enter -Duser.language=es -Duser.country=ES in the VM Options field.
Click OK
Right-click the InternationalizeDemo project and choose Run Project
The IDE runs the FindDialog dialog in the Spanish locale.
Internationalizing an Entire Project
Generally, we have several files in the default locale and we are asked to
adapt them in order to be translated to other languages. The Internationalization
Wizard is the perfect tool for this task, as it can internationalize multiple
files at once. We will shows this feature on the GUI Form examples project,
which contains the form created in the GUI
Building Tutorial.
Creating a Samples Project
Choose File > New Project or click on the New Project icon in the
IDE toolbar.
In the Categories pane, select the Samples > General node and in
the Projects pane, select GUI Form Examples. Click Next.
Enter GUIFormExamples in the Project Name field and specify
the project location (e.g /space/projects).
Ensure that the Set as Main Project checkbox is selected.
Click Finish.
Preparing a Properties File
Choose File > New File or click the New File icon in the IDE toolbar.
In the Categories pane, select the Other node and in the File Types
pane, choose Properties File. Click Next.
Enter ContactEditor in the File Name field and specify the
src/examples folder as the file location.
Click Finish.
The IDE creates the ContactEditor.properties file and opens
the ContactEditor(default language) file in the Source Editor.
Repeat previous steps to create another Antenna.properties
file.
Invoking The Internationalization Wizard
In the Projects window, right-click the Source Packages node and
choose Tools > Internationalization > Internationalization Wizard.
For demonstration purposes, select Find.java and click Remove Source(s)
button.
Click Next.
Check if the Internationalization wizard offers the correct properties files
examples.Antenna and examples.ContactEditor. If it does
not, use the Select Resource button to choose the correct properties file.
Click Next.
All hard-coded strings are displayed in the last step of Internationalization
wizard and it is possible to decide which of them will come from the properties
file (use checkbox). You can further customize individual keys, values, comments,
and format of replaced strings by clicking on the ellipsis (...) button for
a string.
Click Finish.
Now, the internationalization of source code is finished, other locale
can be added and tested
as it was shown previously.
Internationalizing a Single Form
In this last exercise we will reuse the GUI Form Examples project so we will
internationalize the Find.java form, which we excluded in the previous exercise.
We will invoke the Internationalize dialog box to replace all hard-coded strings
in this file. Finally, we will go through a short demonstration of how to insert
an internationalized string in the source code when you are writing a program.
Using the Internationalize dialog box Box
In the Projects window, right-click Find.java and choose Tools
> Internationalization > Internationalize.
The IDE shows Internationalization dialog box and pre-fills the first hard-coded
string from the Find.java source code .
Click Browse to select a particular properties file or create a new one
(e.g. Find.properties).
You can modify format of replaced string, key, value or comment if it is
necessary. We'll just leave the default values.
Click Replace to confirm the change and move a focus on the next hard-coded
string.
If a hard-coded string does not need to be replaced, click the Skip button
To view a demonstration of using the Internationalize dialog box, click
View Demo
Inserting a Single Internationalized String
In the Projects window, right-click Find.java and choose Edit.
The IDE opens the Find.java file in the Source Editor.
Scroll through the source code and find the main method.
Insert the following line in bold in the main method:
public static void main(String args[]) {
System.out.println();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Find().setVisible(true);
}
});
}
Press Ctrl-Shift-J to invoke Internationalized String Dialog.
For Bundle Name, click the New button, select the Source Packages > examples
folder, and enter Find as the Bundle name. Then click OK. The Bundle
Name field shows examples.Find.
Type Start in the Key drop-down box and Start Find Dialog
in the Value field. Then click OK.
The IDE inserts an internationalized string:
public static void main(String args[]) {
System.out.println(java.util.ResourceBundle.getBundle("examples/Find").getString("Start"));
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Find().setVisible(true);
}
});
}
To view a demonstration of inserting internationalized string,
click
View Demo
Related Topics
For more information see the following links:
Java GUIs and Project Matisse Learning Trail
  A full list of articles, guides and tutorials to creating Java
graphical user interfaces (GUIs) using Project Matisse, the NetBeans GUI builder.