// 
//     Licensed to the Apache Software Foundation (ASF) under one
//     or more contributor license agreements.  See the NOTICE file
//     distributed with this work for additional information
//     regarding copyright ownership.  The ASF licenses this file
//     to you under the Apache License, Version 2.0 (the
//     "License"); you may not use this file except in compliance
//     with the License.  You may obtain a copy of the License at
// 
//       http://www.apache.org/licenses/LICENSE-2.0
// 
//     Unless required by applicable law or agreed to in writing,
//     software distributed under the License is distributed on an
//     "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//     KIND, either express or implied.  See the License for the
//     specific language governing permissions and limitations
//     under the License.
//

= Internationalizing a GUI Form
:jbake-type: tutorial
:jbake-tags: tutorials 
:jbake-status: published
:syntax: true
:icons: font
:source-highlighter: pygments
:toc: left
:toc-title:
:description: Internationalizing a GUI Form - Apache NetBeans
:keywords: Apache NetBeans, Tutorials, Internationalizing a GUI Form

The following tutorial takes you through some of the basic steps of internationalization in the 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.

== Principle of Internationalization

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 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 link:https://netbeans.org/files/documents/4/770/InternationalizeDemo.zip[+InternationalizeDemo.zip+] project to any location on your computer.
2. Choose File > Open Project, 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.

image::images/finddialog_ai18n.png[]


=== Switch Automatic Internationalization On

1. Select the root node in the Navigator Window (named  ``Form FindDialog`` ).

image::images/navigator.png[]


[start=2]
. In the Properties window, select the checkbox in the Automatic Internationalization property.

image::images/properties.png[]


[start=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.

image::images/bundle_new.png[]

If you need to have the  ``Bundle.properties``  file in a different location, you can click the ellipsis (...) button to the right of the Properties Bundle File and choose a location or directly type the path in the property's text field.


[start=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.


[start=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.


[start=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`` 

[start=4]
. Type  ``Find What:``  in the Value field.

[start=5]
. Click OK.

Repeat the previous steps for all the components so that the form looks like the following picture:

[.feature]
--
image::images/finddialog_new_small.png[role="left", link="images/finddialog_new.png"]
--

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 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 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. In the New Locale dialog box, choose  ``es_ES``  from the  ``Predefined Locales:``  combobox.
4. Click OK.

The new locale appears below the  ``Bundle.properties``  node, as shown below:

image::images/addlocale_new.png[]


[start=5]
. In the Projects window, right-click  ``Bundle.properties``  and choose Open.

[start=6]
. Translate individual messages in the correspondent column of the table to a new language (for example, Spanish), as shown below:

[.feature]
--
image::images/bundles_new_small.png[role="left", link="images/bundles_new.png"]
--


[start=7]
. Press Ctrl-S to save your edits.

[start=8]
. Select the  ``FindDialog.java``  tab to display the form you are internationalizing.

[start=9]
. Right-click the root node in the Navigator window and choose Reload Form (alternatively, press Ctrl-R).

[start=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:

[.feature]
--
image::images/finddialog_es_new_small.png[role="left", link="images/finddialog_es_new.png"]
--


=== 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.

[.feature]
--
image::images/prjproperties_small.png[role="left", link="images/prjproperties.png"]
--


[start=4]
. Click OK.

[start=5]
. Right-click the InternationalizeDemo project and choose Run.

The IDE runs the  ``FindDialog``  dialog box in the Spanish locale like shown below.

[.feature]
--
image::images/run_small.png[role="left", link="images/run.png"]
--


== 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 show this feature on the GUI Form examples project, which contains the form created in the link:quickstart-gui.html[+Designing a Swing GUI+] tutorial.


=== Creating a Sample Project

1. Choose File > New Project or click on the New Project icon in the IDE toolbar.
2. In the Categories pane, select the Samples > Java node. In the Projects pane, select GUI Form Examples. Click Next.
3. Enter  ``GUIFormExamples``  in the Project Name field and specify the project location (e.g.  ``/space/projects`` ).
4. Click Finish.

image::images/formexamples.png[]


=== Preparing a Properties File

1. Choose File > New File or click the New File icon in the IDE toolbar.
2. In the Categories pane, select the Other node and in the File Types pane, choose Properties File. Click Next.
3. Enter  ``ContactEditor``  in the File Name field.
4. Click Browse and specify the  ``GUIFormExamples/src/examples``  folder as the file location in the Browse Folders dialog box.
5. Click Select Folder.
6. Click Finish.

The IDE creates the  ``ContactEditor.properties``  file and opens it in the Source Editor.

Repeat previous steps to create another  ``Antenna.properties``  file.

image::images/ceprjprops.png[]


=== Invoking The Internationalization Wizard

1. In the Main menu, choose Tools > Internationalization > Internationalization Wizard.
2. On the first page of the Wizard, click Add Source(s).
3. In the Select Sources dialog box, expand the  ``Source Packages``  >  ``examples``  nodes and control-click the  ``Antenna.java`` ,  ``ContactEditor.java`` , and  ``Find.java``  files to select them.
4. Click OK.

The sources files appear in the first page of the Wizard as shown below:

[.feature]
--
image::images/i18nwizardone_small.png[role="left", link="images/i18nwizardone.png"]
--


[start=5]
. For demonstration purposes, select  ``examples.Find``  and click the Remove Source(s) button.

[start=6]
. Click Next.

[start=7]
. 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.

[.feature]
--
image::images/i18nwizardtwo_small.png[role="left", link="images/i18nwizardtwo.png"]
--


[start=8]
. Click Next.

[start=9]
. Skip page 3 of the Wizard since you are not going to create any fields and modify additional values and click Next.

[start=10]
. 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.

[.feature]
--
image::images/i18nwizardthree_small.png[role="left", link="images/i18nwizardthree.png"]
--


[start=11]
. Click Finish.

Now, the internationalization of source code is finished, other locale can be <<newlocale,added>> and <<testlocale,tested>> as it was shown previously.


== Internationalizing a Single Form

Using automatic I18n features is the easiest way to internationalize a GUI form. But if you don't have the update pack installed, or you also want to internationalize code not generated by the Form Editor, then using the Internationalize window is the way to go. (This feature works with any  ``.java``  file, not just files created with the Form Editor). The next example uses the Internationalization window, which is a part of default Netbeans IDE installation.

In this last exercise we will reuse the GUI Form Examples project and 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

1. In the Projects window, select  ``Find.java``  and choose Tools > Internationalization > Internationalize from the main menu.

The IDE shows Internationalization dialog box and pre-fills the first hard-coded string from the  ``Find.java``  source code .


[start=2]
. Click Select to select a particular properties file or create a new one.

[start=3]
. In the Select Resource Bundle dialog box, enter  ``Find.properties``  in the File Name text field, click Create New and then OK.

[start=4]
. You can modify format of replaced string, key, value or comment if it is necessary. We'll just leave the default values.

[start=5]
. 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.

[.feature]
--
image::images/i18ndialog_small.png[role="left", link="images/i18ndialog.png"]
--


=== Inserting a Single Internationalized String

1. In the Projects window, right-click  ``Find.java``  and choose Edit.

The IDE opens the  ``Find.java``  file in the Source Editor.


[start=2]
. Scroll through the source code and find the main method.

[start=3]
. Insert the following line in bold in the main method:

[source,xml]
----

    public static void main(String args[]) {
			   /* Set the Nimbus look and feel */
			   //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            javax.swing.UIManager.LookAndFeelInfo[] installedLookAndFeels=javax.swing.UIManager.getInstalledLookAndFeels();
			for (int idx=0; idx<installedLookAndFeels.length; idx++)
			if ("Nimbus".equals(installedLookAndFeels[idx].getName())) {
			    javax.swing.UIManager.setLookAndFeel(installedLookAndFeels[idx].getClassName());
				break;
			}
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Find.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Find.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Find.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Find.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        *System.out.println();*
		/* Create and display the form */ 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Find().setVisible(true);
            }
        });
               }
----

[start=4]
. Place the mouse cursor within the parentheses of the  ``System.out.println();``  so that an internationalized string is inserted as a parameter.

[start=5]
. Press Ctrl-Shift-J to invoke Insert Internationalized String dialog box (alternatively, you can choose Tools > Internationalization > Insert Internationalized String from the main menu).

[start=6]
. For Bundle Name, click the Select button, select the  ``Source Packages > examples``  folder, and enter  ``Find``  as the Bundle name in the File Name text field. Then click OK. 
The Bundle Name field of the Insert Internationalized String dialog box shows  ``examples.Find`` .

[start=7]
. Type  ``Start``  in the Key drop-down box and  ``Start Find Dialog``  in the Value field. Then click OK.

image::images/insi18nstring.png[]


[start=8]
. The IDE inserts an internationalized string:

[source,xml]
----

    public static void main(String args[]) {
			   /* Set the Nimbus look and feel */
			   //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            javax.swing.UIManager.LookAndFeelInfo[] installedLookAndFeels=javax.swing.UIManager.getInstalledLookAndFeels();
			for (int idx=0; idx<installedLookAndFeels.length; idx++)
			if ("Nimbus".equals(installedLookAndFeels[idx].getName())) {
			    javax.swing.UIManager.setLookAndFeel(installedLookAndFeels[idx].getClassName());
				break;
			}
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Find.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Find.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Find.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Find.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
*        System.out.println(java.util.ResourceBundle.getBundle("examples/Find").getString("Start"));*
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Find().setVisible(true);
                }
            });
               }
----
