Tuesday, January 19, 2010

Dynamic Internationalization in Java (Multilingual Support)

Getting Started
Dynamic Internationalization in Java (Multilingual Support)

Dynamic internationalization is the process of setting up your program so that the locale can be changed “on-the-fly” with the program running. Otherwise, with static internationalization, the locale will load once (at program start-up) based on the settings of your operating system. To change the language you will have to reboot the OS or, at the least, restart the application.

Dynamic internationalization is useful if you will have multiple users from different language backgrounds accessing the same application session. Dynamic Internationalization will allow each to choose their preferred language while they are using the program.

Dynamic internationalization deals not only with language, but also locale-sensitive formats such as calendars, dates, times, numbers, currency, etc.

The following example is one way to accomplish dynamic internationalization. There are many other possible ways to achieve the same desired result.

1) Download softsmithy.jar and add it to your library in Netbeans:

2) Create a ‘New Project’ in Netbeans. Choose ‘Java Application’ and name the project ‘dynamicinternationalization’. Uncheck the ‘Create Main Class’.

3) Right-click on your package and add a new ‘Java Class’ and name it ‘DIExample’.

4) Right-click on your project and add a new ‘Java Package’ and name it ‘dynamicinternationalization.resources’

5) Right-click on this new ‘dynamicinternationalization.resources’ bundle and add a new ‘Properties File’ and name it ‘DIExample’.

6) Repeat step 5 again for each language you are adding. For our example please create the following property files: ‘DIExample_en’, ‘DIExample_fr’, ‘DIExample_ja’

7) Open the ‘DIExample.properties’ file and enter the following code:
langLabel = to eat
8) Right-click on the ‘DIExample.properties’ and choose open and fill in each language translation.

Finally open the ‘DIExample.java’ file and enter the following code:
package dynamicinternationalization;

import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import org.softsmithy.lib.swing.LocaleCellRenderer;
import org.softsmithy.lib.swing.XDefaultListCellRenderer;
import org.softsmithy.lib.util.LocaleDisplay;
import org.softsmithy.lib.util.Locales;

public class DIExample extends javax.swing.JDialog {

private javax.swing.JLabel langLabel;
private javax.swing.JComboBox langList;
private Locale[] locales;
private Locale currentLocale = Locale.ENGLISH;

public DIExample(java.awt.Frame parent, boolean modal) {
super(parent, modal);

locales = new Locale[] {Locale.ENGLISH,
new Locale("ja", "JP"),
new Locale("fr", "CH"),};

langList.setRenderer(new XDefaultListCellRenderer(new LocaleCellRenderer()));


private void updateModel() {
Locale[] l = Locales.sort(locales, LocaleDisplay.NAME, currentLocale);
DefaultComboBoxModel model = new DefaultComboBoxModel(l);
private void updateLocale() {

private void langListActionPerformed(java.awt.event.ActionEvent evt)
currentLocale = (Locale) langList.getSelectedItem();

Locale localizacion = Locale.US;
ResourceBundle bundle = ResourceBundle.getBundle("dynamicinternationalization.resources.DIExample",localizacion);
langLabel.setText(bundle.getString("langLabel")); // NOI18N


else if(currentLocale.getLanguage().equals("ja")){
Locale localizacion2;
localizacion2 = new Locale("ja");
ResourceBundle bundle = ResourceBundle.getBundle("dynamicinternationalization.resources.DIExample",localizacion2);
langLabel.setText(bundle.getString("langLabel")); // NOI18N

else if(currentLocale.getLanguage().equals("fr")){
Locale localizacion3;
localizacion3 = new Locale("fr");
ResourceBundle bundle = ResourceBundle.getBundle("dynamicinternationalization.resources.DIExample",localizacion3);
langLabel.setText(bundle.getString("langLabel")); // NOI18N


private void initComponents() {

langList = new javax.swing.JComboBox();
langLabel = new javax.swing.JLabel();


langList.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("dynamicinternationalization.resources.DIExample"); // NOI18N

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
.add(langList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 146, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(langLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 102, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(124, Short.MAX_VALUE))
.add(langList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(257, Short.MAX_VALUE))


public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
DIExample dialog = new DIExample(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {

Internationalization links:

Sunday, January 17, 2010

Getting Started (Part I - Downloading the IDE)

Getting Started

Part I - Downloading the IDE

Welcome to the “Java - Getting Started” blog. The goal of this site is to give the novice Java programmer practical solutions to some of the typical roadblocks encountered when trying to create a Java database application for the first time. This site is more geared towards the ends (how to make it work) than the means (the detailed explanations of why it works).

Why use Java?

A Java application can run on any Java Virtual Machine (JVM) regardless of the computer architecture (i.e. it can run on any operating system - Windows, Mac, Linux as long as that computer has the JVM installed). Creating a database for your business using Java will give you much more flexibility compared to other standard database software programs (such as Microsoft Access).

Downloading the IDE

First and foremost we need to download the tool we will be using to create our Java program. We will be using the Netbeans IDE to help us write our Java database program. An IDE (Integrated Development Environment) is a software application that allows the programmer to visually develop the application.

1) Download and install the Netbeans IDE:


2) Be sure to check out the Netbeans tutorial page. This page has many great tutorials that I will refer to in future posts:


Stay tuned for future posts on topics such as:

- JDBC - Using the “Embedded Derby” driver

- Creating a Java Desktop Application with Multiple Database Tables

- Dynamic Internationalization - How to allow users to dynamically switch the language of the program while it is running

- Using the JCalendar Java date chooser bean

- Creating Reports - Using iReport and Jasper Reports to create your database reports

- Exporting your reports to Excel and .PDF