?? internationalization.html
字號(hào):
<html>
<head>
<TITLE>jGuru: Internationalization</TITLE>
<style>
<!--
a{text-decoration:none;}
a:hover{color:6699cc; }
body{background-color:white;}
-->
</style>
</head>
<BODY BACKGROUND="images/watermark.gif" BGCOLOR="#FFFFFF" LINK="#006699" VLINK="#006699">
<table width=100% cellpadding=0 cellspacing=0 border=0>
<tr><td bgcolor="#336699" colspan=2 align=right><img src="images/topbar.gif" width=600 height=14 alt="" border="0"></td></tr>
<tr><td colspan=2> </td></tr>
<tr valign=top>
<td valign="top" width="140"><div align="center"><img src="images/logo.gif" width=78 height=90 alt="" border="0"></div>
<font color="#0000FF" size="1" face="verdana,arial,helvetica"><strong><br>
<a href="contents.html">COURSE NOTES</a><br><img src="images/spacer.gif" width=1 height=4 alt="" border="0"><br><img src="images/spacer.gif" width=1 height=4 alt="" border="0"><br><a href="index.html">MODULE INTRO</a><br><img src="images/spacer.gif" width=1 height=4 alt="" border="0"><br>
<!-- AFTER GUTTER -->
<img src="images/spacer.gif" width=140 height=1 alt="" border="0">
</strong></font>
</td>
<TD valign=top align=left>
<!-- START CONTENT -->
<H1><strong>Internationalization</strong></H1>
<H2><a name="locIntro">What is Localization?</a></H2>
<p><em>Localization</em> is the ability to customize a program to the user's language (e.g., for prompts and
error messages) and display characteristics (e.g., for how to display times and dates).
The different customizations are defined by individual areas or regions, each called
a <tt><a href="http://java.sun.com/products/jdk/1.2/docs/api/java/util/Locale.html">Locale</a></tt>.</p><p>Before you can localize any programs, you need to isolate the locale dependent pieces. This
isolation process is called <em>internationalization</em>. In the simplest case, this involves the moving of all
<tt>String</tt> constants into something called a
<tt><a href="http://java.sun.com/products/jdk/1.2/docs/api/java/util/ResourceBundle.html">ResourceBundle</a></tt>. However, resource bundles are not
restricted to just text messages and may include images, sounds, and numeric information. And this
isn't the only step involved.</p><H2><a name="locLocale">The Locale Object</a></H2>
<p>A <tt>Locale</tt> is specified by adding a trailing string to an identifier that specifies a <tt>ResourceBundle</tt>. The first three letters are an underscore ('_') and a two character language code (lowercase). The next three are an underscore and
a two character country code (uppercase). This is because not everyone speaking the same language speak the same dialect. For instance, Quebec and France have different variations on the French language. <tt>_fr_FR</tt> is France French, while <tt>_fr_CA</tt> is French Canadian.</p><p>A locale, then, defines a specific local area, without specifying any other details about that area's needs. The class <tt>Locale</tt> resides in the <tt>java.util</tt> package.
Several classes provide methods that return instances of <tt>Locale</tt>.</p><p>
To construct a locale manually, use code resembling
</p><pre>
Locale l =
new Locale(String lang, String country,
String variant);
</pre><p>or</p><pre>
Locale l =
new Locale(String lang, String country);
</pre><p>Every runtime has a default locale, which you can acquire using</p><pre>
Locale l = Locale.getDefault();
</pre><p>You can also set the default locale by setting system properties; however, this is not recommended, since the user may have set his own default <tt>Locale</tt>, and this would override those settings.</p><pre>
// dangerous
Properties p = System.getProperties();
p.put("user.language", "fr");
p.put("user.region", "CA");
System.setProperties(p);
</pre><p>Some objects may set their own locale, which would override the default locale. Those objects (including all AWT Components) implement a <tt>getLocale()</tt> method.
If no locale is set, it returns the default locale.</p><p>To retrieve a list of all the available locales, use
<tt>Locale list[] = DateFormat.getAvailableLocales();</tt>.
The <tt>locale.toString()</tt> method returns the concatenated locale value, e.g.
<tt>"_fr_FR"</tt> (French / France),
<tt>"_fr_CA"</tt> (French / Canada), or
<tt>"_en_US"</tt> (English / United States).
To retrieve the individual locale "components" as strings, use
<tt>locale.getLanguage()</tt>,
<tt>locale.getCountry()</tt>, and
<tt>locale.getVariant()</tt>.
</p><H2><a name="locDemo">Why You Need It</a></H2>
<p>
<center><img src="images/class.gif"/>
</center>
</p><p>Imagine taking this program and trying to run it in Italy and Finland. While it will run, there are a few internationalization issues:</p><ul>
<li>All text labels are in English</li>
<li>The Date display format is specific to the United States</li>
<li>The Money display format is specific to US $</li>
<li>The map image and city listed are in the United States</li>
</ul><p>You'll now see how you can internationalize the application.</p><H2><a name="locResBund">Resource Bundles</a></H2>
<p>In order to localize labels, you need to pull them all out and do lookups at runtime.
The means to do this is through the <tt>ResourceBundle</tt> class. Resource bundles
provide a simple key-value lookup and may be provided via <tt>.class</tt> or <tt>.properties</tt> files.
(Other means can also be provided, however you would have to do all the work yourself.)</p><p>To provide a lookup list via a class, you subclass
<tt><a href="http://java.sun.com/products/jdk/1.2/docs/api/java/util/ListResourceBundle.html">ListResourceBundle</a></tt> and override the
<tt>getContents</tt> method to return an <tt>Object[][]</tt>. For each pair of elements in the array, the first would be the lookup key and the second the value for the key for the given <tt>Locale</tt>.</p><pre>
import java.util.ListResourceBundle;
public class training-labels-bundle
extends ListResourceBundle {
static final Object[][] contents = {
{ "Date", "Date: " },
{ "Cost", "Cost: " },
{ "Location", "Location: " },
{ "Title", "Training" }
};
protected Object[][] getContents() {
return contents;
}
}
</pre><p>In the <tt>training-labels-bundle</tt> defined above, four lookup labels define four properties:</p><ul>
<li>Date</li>
<li>Cost</li>
<li>Location</li>
<li>Title</li>
</ul><p>You would then use this bundle to lookup resources in a two step process: load the resource list and search it. To load the list, use <tt>ResourceBundle.getBundle()</tt>. This will load the bundle for the appropriate <tt>Locale</tt>. Since there is only one so far, and it is the default, the <tt>training-labels-bundle</tt> one is what is loaded.</p><pre>
Locale locale = getLocale();
ResourceBundle labels = ResourceBundle.getBundle (
"training-labels-bundle", locale);
</pre><p>Once you have a bundle, you lookup entries via its <tt>getString()</tt> method:</p><pre>
p.add (new Label (labels.getString("Date")));
</pre><p>The program for the figure shown above can now include the following:</p><pre>
ResourceBundle labels = ResourceBundle.getBundle (
"training-labels-bundle", locale);
p.add (new Label (labels.getString("Date")));
p.add (new Label (labels.getString ("Cost")));
p.add (new Label (labels.getString ("Location")));
add (new Label (labels.getString ("Title"),
Label.CENTER), BorderLayout.NORTH);
}
</pre><p>Then, if you want the program to support Italy and Finland locales to display localized labels, you would have to create two new classes: <tt>training-labels-bundle-it</tt> and <tt>training-labels-bundle-fi</tt>. In these classes, only those entries that were different
in the new locale would need to be specified in the bundle. Now that three bundles are defined, based on the current locale setting, the <tt>getBundle()</tt> method would fetch and load the appropriate class.</p><p>For Finland, the appropriate bundle would be:</p><pre>
public class training-labels-bundle-fi
extends ListResourceBundle {
static final Object[][] contents = {
{ "Date", "Päivämäärä: " },
{ "Cost", "Hinta: " },
{ "Location", "Sijainti: " },
{ "Title", "Koulutus" }
};
protected Object[][] getContents() {
return contents;
}
}
</pre><p>For Italy, the appropriate bundle would be:</p><pre>
public class training-labels-bundle-it extends ListResourceBundle {
static final Object[][] contents = {
{ "Date", "Data: " },
{ "Cost", "Costo: " },
{ "Location", "Posto: " }
};
// The Training title remains the same (or addestramento)
protected Object[][] getContents() {
return contents;
}
}
</pre><H3><a name="locPropRes">Property Resource Bundles</a></H3>
<p>In addition to providing resource bundles as <tt>ListResourceBundle</tt> classes, you can provide <tt><a href="http://java.sun.com/products/jdk/1.2/docs/api/java/util/PropertyResourceBundle.html">PropertyResourceBundle</a></tt>s. These are property files where each resource is specified on its own line as text, as in <tt>Date=June 3, 2000</tt>. Property resource bundle files are named <tt>Bundlename</tt><i>_Localization</i><tt>.properties</tt>.</p><p>The previously described program requires three properties for the actual date, cost, and location map. You can create another bundle in filename <tt>training-settings-bundle.properties</tt> for this information. Since this information is likely to change more frequently, it makes sense to be editable by a human, without requiring recompilation.</p><pre>
Date=June 3, 2000
Cost=1200.00
Map=SMarea.gif
</pre><p>For Finland, the appropriate bundle file would be named <tt>training-settings-bundle-fi.properties</tt> and may include:</p><pre>
Cost=6240
Map=ilmakuva.gif
</pre><p>For Italy, the appropriate bundle file would be named <tt>training-settings-bundle-it.properties</tt> and may include:</p><pre>
Cost=2168400.00
Map=Milan.gif
</pre><p>This assumes the date for all three classes would be the same at the different locations.</p><p>Access to <tt>PropertyResourceBundle</tt> members is identical to <tt>ListResourceBundle</tt>:</p><pre>
ResourceBundle settings = ResourceBundle.getBundle(
"training-settings-bundle", locale);
String date = settings.getString("Date");
String cost = settings.getString("Cost");
String filename = settings.getString("Map");
</pre><H2><a name="locFormats">Locale-Specific Formats</a></H2>
<p>Once you've pulled all your string constants into resource bundles, its not time to figure out how to display that information. For instance, dates, numbers, and messages may be displayed differently based upon where one is located.</p><H3><a name="locDate">Date Formatting</a></H3>
<p>Display formats of dates are not universal. Is "1/2/99" January 2nd or February 1st? It depends.
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -