In Java, the preferred method for handling multilingual text is to use ResourceBundles for simple text and ListResourceBundles for more complex data types. As I alluded to in my Isolating Locale-specific Text in International Java Applications article, that approach has drawbacks.
One prevalent criticism against it is that the mechanism
ResourceBundle.getBundle() assumes that you will have a different class
for every locale, country and variant you want to provide. Assuming that you get
your resource bundle using the
ResourceBundle.getBundle() method, this negates
the possibility of dynamically adding a new locale to an existing application
even if you store the keys and values in a database.
Unfortunately, many popular frameworks such as JSTL and Struts locate bundles using this method. What is required is an i18n mechanism that allows us to add a new localization without having to modify application code. In this article, I demonstrate a way to implement just such a mechanism using a database-driven ResourceBundleControl.
Communicating with a MySQL Database
Installation instructions for the database server are provided in Section 2 of the database server documentation, entitled "Installing MySQL." Unless you want to work with source code, you can skip down to the relevant section on "Installing a Binary Distribution."
In Windows, log on as an administrator and execute the following instructions from the database server documentation to install the MySQL database server on your computer:
- Unzip the distribution file to a temporary directory.
- Run the setup.exe program to begin the installation process.
- If you want to install MySQL into a location other than the default directory (C:\mysql), use the Browse button to specify your preferred directory. If you do not install MySQL into the default location, you will need to specify the location whenever you start the server. The easiest way to do this is to use an option file, as described in Section 18.104.22.168, "Preparing the Windows MySQL Environment." I recommend allowing the software to be installed in the default directory of C:\mysql.
Other operating systems vary only slightly.
- For Mac OS X, see section 2.7.1. in the reference manual, entitled "Installing MySQL Using the Installation Package. "
- For Linux, see section 2.6., entitled "Installing MySQL on Linux."
- For Solaris, see section 2.8., entitled "Installing MySQL on Solaris."
The full list of operating systems is found in Chapter 2. Installing and Upgrading MySQL.
Further down on the MySQL Downloads page, you'll find the link for MySQL Connector/J, which is the official JDBC driver for MySQL. For those not familiar with JDBC, the JDBC API is designed to enable you to write a single Java program and to use it to manipulate the data in a variety of different SQL database servers without having to modify and/or recompile your program.
This zip/tar file encapsulates 194 individual files in different folders including source code files, class files, PDF files, XML files, JAR files, license files, files with no extensions, a manifest file, HTML files, and many others. It also contains several Java programs in a folder named testsuite that can be used to test your installation. In addition, these programs illustrate a variety of database operations using JDBC, which you may find beneficial as example programs. Fortunately, you can ignore all but one of these files when installing the connector software.
There are two ways to
install the driver, i.e. put the "mysql-connector-java-[version]-bin.jar"
file in your classpath. You can either add the full path to the file to your CLASSPATH
environment variable, or copy the .jar file to your
The Java Connection Code
Connecting to the database is a three-step process:
- Register the JDBC driver with the JRE.
- Define the URL of the database server. (The URL is comprised of the protocol, server, port number and the database name.)
- Establish a connection, supplying the URL, ID, and password.
Here is the code required to establish a connection to the database:
The db_resource_bundle_article Database
Use the code below to create a database called db_resource_bundle_article, with one table called resource_bundle. It will also populate the table with 11 rows of test data. Depending on your role, you may or may not have much experience with database objects and Data Definition Language (DDL) syntax. If you're relatively new to MySQL or databases in general, you might want to take a look at the MySQL documentation on the MySQL Command Line Tool. It can even be used to execute SQL statements from a file.
The above code will produce the following table. In it, are two bundles: a
default and a label_bundle. There are three fields to store the local information
of country code, language code, and the variant code, such as those found in
a local string:
en_CA_WINDOWS. The final two columns of
contain the name/value pairs that comprise a property: