Internationalize (i18n) java apps with NetBeans
Hello everyone, welcome to my first tutorial of netbeans and java programming, this time I am explaining the easiest way to internationalize a java application using NetBeans IDE 6.5, this is a great IDE for java developing, because it’s free, very friendly and has a lot of documentation, I also need to say that this tutorial requires basic-medium skills in Java programming, so le’s getting started shall we.
Ok first of all you need to know some basics about internationalization (i18n, this is the most common way to express internationalization because there are 18 letters between i and n within the whole word).
So i18n is the way an application can be used in many countries without the need of recompile the entire application, this is very easy to understand so I think you are getting the whole point of this.
The process of internationalize an application is by using locale files and properties files this files are organized in key/value arrays, you will have one properties file for each language you want to use.
So no more theory for now and let’s get started:
1.- Open the NetBeans IDE and then create a new project, select Java Desktop application so we can start having a simple GUI form, then click next.

(I am using a spanish version of NetBeans, but I think you will not have so much troubles to understand)
2.- Next you have to give a name to the project, we will call it “i18nTut”, you can choose another name if you want, and we will save the project directly on the root of your file system, then click on finish to create the project.

3.- Once you have done this you will have a project structure like this:

Where “Paquetes de Fuentes” (source packages) is where we will add all the files we need for this tutorial, I will not going to explain the rest of the structure because this tutorial should be compact and focus only on what we are explaining on this document.
4.- Right click on <paquete predeterminado> (default package) and select New -> Java Package (Paquete Java), then call this package “i18nTuto”, after that select your newly created package and again do right click and select New->JFrame form.

After that you need to put a name to your new JFrame form, you can put whatever you want, I will use “JFrameSample” then click finish and you will have a new form in the design layout with the component palette at your right.

So let’s add some components to our form, drag and drop a JLabel and two JButton to the JFrame form, then modify the properties of each component like this:
JLabel Name: jlblTitle
JLable Text: “Click on the Hello button”
JButton1 Name: jbtnChangeLang
JButton1 Text: Change language
JButton2 Name: jbtnHello
JButton2 Text: “Hello”
Once you have done this you will have something like this:

5.- Now in the inspector window select JFrameSample form and then in the properties window, check the box Automatic Internationalization (Internacionalización automática).

After this the IDE will create automatically a bundle.properties file.
So in the project inspector window, double click on the bundle.properties file so it will open and will show you all the text for each component you have added to the JFrame form, organized as key=value layout.
So now let’s create a new Locale to use, on the Inspector window select JFrameSample form and then in the properties window look for the Design Locale property (Diseñar Localización), click on the ellipsis button (+) after that a window like this will appear:

6.- Write “es” in the language code (Código de idioma) and write MX in the country code (Código de país) then click on Ok (Aceptar) after that you will have an es_MX.properties file, like this:

Then in the project window select the Bundle.properties file, right click and select open (Abrir) so you will have a window like this:

So you can edit and write the text for the Spanish language, I already did it as shown in previous image.
7.- Now let’s add more spicy to our small application, select the Hello Button and right click then select Events->Action->actionPerformed.

This option will take us to the code window to write code when the button is clicked, so once we get to the code window write this:

8.- Select the “text” inside the setText method, once you have done this, let’s create a simple string e also ready for internationalization, so go to the Tools menu (menú Herramientas)->Internationalization->Insert Internationalizated String (Insertar la cadena internacionalizada)
or you can press ctrl. + shift + J to open a dialog box.


In this dialog box in the key field (Clave) write “Salute” and in the value (Valor) write “Hello Everyone”, then click Ok (Aceptar) then you will have something like this:

9.- After that you just need to open the bundle.properties file and add the translation for the Spanish locale, after that you can try the whole application by clicking on the Run application button (Green triangle).
Ok so let’s review what we have made so far:
We create a new project; add some components and we create a bundle.properties file.
We create a new es_MX locale file to add Spanish to our application.
Create an internationalized string to show a message in different languages
Now let’s add more options to our application so you can change the locale anytime
10.- So first we need to create a new properties file to store settings and configuration strings for our application, to do that simply do right click on the package of our project in the project window and then New->other…

In the next window select other as category and then in file type select properties file, then put a name to the file, we will name it “conf.properties” then save it to the “src” directory and click finish.
After that you will prompt to the edit area of the conf.properties file, so just write this line:
“lang=en_US” (without the quotation marks) and save.
11.- Now let’s add code, go to the change language button in the design layout and add a new action performed method, I already describe how to do it, when you are prompted to the code layout write this:
java.util.ResourceBundle configs = java.util.ResourceBundle.getBundle(”i18nTuto/conf”);
String langs = configs.getString(”lang”);
Properties prop = new Properties();
if (langs.equals(”en_US”)){
prop.setProperty(”lang”, “es_MX”);
System.out.println(”Language changed to espanish”);
}
else if(langs.equals(”es_MX”)){
prop.setProperty(”lang”, “en_US”);
System.out.println(”Language changed to english”);
}
try {
prop.store(new FileOutputStream(”src/i18nTuto/conf.properties”), null);
System.out.println(”Changed saved sucessfully restart the application to see the changes”);
} catch (IOException e) {}
Basically this code gets the properties file, and check for the default language written in that file, then if it’s English it change it to Spanish and vice versa, then after that it saves the changes.
12.- You may find that the IDE marked some errors in red because you need to add some packages like IO or properties, so to correct that simply do right click on any part of the code and select the “fix imports” or press ctrl. + shift + I then NetBeans automatically will add all the libraries and packages needed.
Now in the code layout look for the main method and write this code at the beginning of the method:
java.util.ResourceBundle configs = java.util.ResourceBundle.getBundle(”i18nTuto/conf”);
// Get default locale
Locale locale = Locale.getDefault();
// Set the default locale to custom locale
locale = new Locale(configs.getString(”lang”));
Locale.setDefault(locale);
So that’s it, now you can test your application by running it, if you press the change language button you will see that in the output window a message will appear describing the operations:

I hope this tutorial could be of some help to you, and if you have suggestions or comments please feel free to make them, so then, see you on the next tutorial.
Your tutorial is very good, I just have one question. You can send to me your proyect in NetBeans already to see and to learn better?
My e-mail is VScorpion.Black@gmail.com
I am a java programmer novate. And all this facine to me. But I cannot learn all these of internationalization.
So, thank you very much.
My english is not perfect, I live in Mexico.
Hey if I want to change the language interface (JFrame) at the same time, as seriously?
actually, I want to change the location in runtime, with a local selector
thanks ?
Hi thanks for your post, you may need to use a class loader if you want to reload the configuration during runtime, i have never tried it before but i think you can find good tutorials just google them
, besides it will only work for what is on your configuration.properties file.
You can’t change the locale on your application because the locale is under the java virtual machine, you can change the locale configuration setting it as parameters on the ant script or the manifest of the jar file so everythime the jar or the application start it will change the jvm configuration.