Internationalization (I18N) is the process of designing an application so that it can be adapted to various languages and regions without engineering changes. Localization (L10N) is the use of locale-specific language and constructs at run time.
Most operating systems allow to indicate their locale or to modify it.
For instance Windows NT does this through the control panel, under the
Regional Option icon. In Java, you can get the Locale object that
matches the user's control-panel setting using
myLocale
= Locale.getDefault();
You can also create Locale objects for specific places by indicating
the language and country you want, such as
myLocale
= new Locale("fr", "CA");
for "Canadian French."
The next example creates Locale objects for the English language in the United States and Great Britain:
bLocale = new Locale("en", "US");The strings you pass to the Locale constructor are two-letter language and country codes, as defined by ISO standards (put here reference)
cLocale = new Locale("en", "GB");
The ResourceBundle class is an abstract class that provides an easy way to organize and retrieve locale-specific strings or other resources. It stores these resources in an external file, along with a key that you use to retrieve the information. You'll create a ResourceBundle for each locale your Java program supports.
Resource bundles inherit from the ResourceBundle class and contain localized elements that are stored external to an application. Resource bundles share a base name. The base name TeT_Bundle, to display transactional messages such “Transaction Commited”, might be selected because of the resources it contains. Locale information further differentiates a resource bundle. For example, TeT_Bundle_it means that this resource bundle contains locale-specific transactional messages for Italian.
To select the appropriate ResourceBundle, invoke the ResourceBundle.getBundle method. The following example selects the TeT_Bundle ResourceBundle for the Locale that matches the French language, the country of Canada.
Locale currentLocale = new Locale("fr", "CA");Java loads your resources based on the locale argument to the getBundle method. It searches for matching files with various suffixes, based on the language, country, and any variant or dialect to try to find the best match. Java tries to find a complete match first, and then works its way down to the base filename as a last resort.
ResourceBundle introLabels = ResourceBundle.getBundle("TeT_Bundle", currentLocale);
You should always supply a base resource bundle with no suffixes, so that your program will still work if the user's locale does not match any of the resource bundles you supply. The default file can contain the U.S. English strings. Then you should provide properties files for each additional language you want to support.
Basically, a resource bundle is a container for key/value pairs. The key is used to identify a locale-specific resource in a bundle. If that key is found in a particular resource bundle, its value is returned.
The jdk API defines two kinds of ResourceBundle subclasses -- the PropertyResourceBundle
and ListResourceBundle.
A PropertyResourceBundle is backed by a properties file. A properties
file is a plain-text file that contains translatable text. Properties files
are not part of the Java source code, and they can contain values for String
objects only. A simple default properties file, named hpts_Bundle.properties,
for messages sent by HPTS could be.
# Sample properties file for demonstrating PropertyResourceBundleThe equivalent properties file, hpts_Bundle_fr_FR.properties, for French would be:
# Text to inform on transaction outcomes in English (by default)
trans_committed= Transaction Committed
trans_rolledback= Transaction Rolled Back
# …
# Sample properties file for demonstrating PropertyResourceBundle
# Text to inform on transaction outcomes in French
trans_committed = La Transaction a été Validée
trans_rolledback = La Transaction a été Abandonnée
# …
The ListResourceBundle class manages resources with a convenient list.
Each ListResourceBundle is backed by a class file. You can store any locale-specific
object in a ListResourceBundle. Resources in a ListResourceBundle are not
limited to text strings, as are the resources in a PropertyResourceBundle.
For example, images are one example of resources that can be localized,
but can't be stored as text strings. To add support for an additional
Locale, you create another source file and compile it into a class file
as described below where the first file, hpts_Bundle.java defines the default
English messages and the second file, hpts_Bundle_fr_FR.java, its equivalent
for French.
import java. util.*;
public class TeT_Bundle extends ListResourceBundle { public Object [][] getContents() { return contents; } static final Object
[][] contents = {
|
import java. util.*;
public class TeT_Bundle extends ListResourceBundle { public Object [][] getContents() { return contents; } static final Object
[][] contents = {
|
Note that the ResourceBundle class is flexible. If you first put your locale-specific String objects in a PropertyResourceBundle and then later decided to use ListResourceBundle instead, there is no impact on the code.
import com.hp.mw.orbportability.*;
import com.hp.mw.ts.jts.extensions.*; import com.hp.mw.ts.jts.common.jtsPropertyManager; import com.hp.mw.ts.jts.common.Environment; import com.hp.mw.ts.jts.OTSManager; import com.hp.mwlabs.ts.jts.ORBManager; import org.omg.CosTransactions.*;
public class TransactionTest1
try
try
myOA.destroy();
|
In the following example the language code is fr (French) and the country code is FR (France), so the program displays the messages in French:
% java TransDemoClient fr FRRather to specify explicitly the language to be used to display messages, a property variable can be
La Transaction a été validée