?? internationalization.html
字號:
Java supports the parsing and display of dates through the
<tt><a href="http://java.sun.com/products/jdk/1.2/docs/api/java/text/DateFormat.html">DateFormat</a></tt> class. There are four formats supported for
dates. Each represents a constant in the class:</p><ul>
<li>SHORT - Completely numeric: 1/2/99</li>
<li>MEDIUM - Abbreviated month: Feb 1, 1999</li>
<li>LONG - Full month name: February 1, 1999</li>
<li>FULL - Completely specified: Saturday, February 1, 1999 AD</li>
</ul><p>The <tt>training-settings-bundle</tt> stored the contents of the "Date" string in the US/Long format. So, when converting from a text string to a <tt>Date</tt> object with the help of the <tt>DateFormat</tt> class, you must specify what format the input text is for the date:</p><pre>
Locale aLocale = getLocale();
ResourceBundle settings = ResourceBundle.getBundle (
"training-settings-bundle", aLocale);
String dateString = settings.getString("Date");
DateFormat dfInput = DateFormat.getDateInstance (
DateFormat.LONG, Locale.US);
Date inputDate;
try {
inputDate = dfInput.parse(dateString));
} catch (ParseException e) {
inputDate = null;
}
</pre><p>Once you have a <tt>Date</tt>, you need another <tt>DateFormat</tt> to specify how to print out the date. This allows you to format the date for the local display properties (for things like month/day order and '.' vs. '/' as the separator).</p><pre>
DateFormat dfOutput = DateFormat.getDateInstance (
DateFormat.SHORT, aLocale);
String dateLabelString;
try {
dateLabelString = dfOutput.format (
dfInput.parse(dateString));
} catch (ParseException e) {
dateLabelString = dateString;
}
</pre><p>For June 3, 2000, this results in the following values:</p><ul>
<li>US - 6/3/00</li>
<li>Italy - 03/06/00</li>
<li>Finland - 03.06.00</li>
</ul><H3><a name="locNumber">Number/Currency Formatting</a></H3>
<p>The <tt><a href="http://java.sun.com/products/jdk/1.2/docs/api/java/text/NumberFormat.html">NumberFormat</a></tt> class works similarly as
the <tt>DateFormat</tt> class. You can get an instance for a specific <tt>Locale</tt> via <tt>getNumberInstance()</tt> for normal number format, <tt>getCurrencyInstance()</tt> for monetary values, and <tt>getPercentInstance()</tt> for localized percentage format. For this example, the cost of the class needs to be displayed, so the <tt>getCurrencyInstance()</tt> method is needed to display the output. However, in the resource file, this value was stored as a raw number; so for input the <tt>getNumberInstance()</tt> mehtod needs to be used.</p><pre>
String costString = settings.getString ("Cost");
NumberFormat nfInput =
NumberFormat.getNumberInstance (Locale.US);
NumberFormat nfOutput =
NumberFormat.getCurrencyInstance (aLocale));
String costLabelString;
try {
Number n = nfInput.parse (costString);
costLabelString = nfOutput.format (n.longValue());
} catch (ParseException e) {
costLabelString = costString;
}
</pre><p>For 1200.00, this results in the following values:</p><ul>
<li>US - $1,200.00</li>
<li>Italy - L. 1.200,00</li>
<li>Finland - 1 200,00 mk</li>
</ul><p>Notice that it does include local currency symbols. However, it doesn't do currency conversions. It is your responsibility to place different costs for each locale in the resource bundle.</p><H3><a name="locMessages">Localized Messages</a></H3>
<p>While resource bundles allow you to get localized message information, if an error message -- or any message the user might see, such as dialog boxes -- needs to be ordered differently based upon the locale involved, just using resource bundles is not a sufficient solution. The <tt><a href="http://java.sun.com/products/jdk/1.2/docs/api/java/text/MessageFormat.html">MessageFormat</a></tt> class provides the means to properly create these types of messages. A resource bundle would contain the format of the message, while the run-time circumstances would fill in the specifics.</p><p>For instance, one part of the United States might like to see error messages of the form "I/O Exception while loading: Foobar.java,"
while another might like "Foobar.java loaded unsuccessfully: I/O Exception."
Or, the message
<pre>
The disk G contains 3 files. (English)
</pre>
is translated into French as
<pre>
Il y a 3 fichiers sur le disque G. (French)
</pre>
Note that the order of the parameters "G" and "3" is reversed.
</p><p>
To do this, one would first need to create two resource bundles (as above), one for each part of the country. In bundle one, the format for this specific error would be defined as: "{0} while loading: {1}", while the second would have: "{1} loaded unsuccessfully: {0}". The <tt>{0}</tt> represents a position holder to substitute arguments into the message. The position holders start at 0 and increase.</p><p>When it comes time to actually create the message to display, with the arguments filled in, you use the <tt>MessageFormat.format()</tt> method. This takes two arguments, the first being the message to format, the second being an <tt>Object []</tt> of the arguments. The
following demonstrates everything:</p><pre>
import java.text.*;
public class format-it {
public static void main (String args[]) {
String format1 = "{0} while loading: {1}";
String format2 = "{1} loaded unsuccessfully: {0}";
String exceptionName = "I/O Exception";
String filename = "Foobar.java";
Object [] fmtargs = {exceptionName, filename};
System.out.println (
MessageFormat.format (format1, fmtargs));
System.out.println (
MessageFormat.format (format2, fmtargs));
}
}
</pre><p>The output of running follows:</p><pre>
Foobar.java while loading: I/O Exception
I/O Exception loaded unsuccessfully: Foobar.java
</pre><p>Besides just plain numbers as arguments for the formatting, you can specify datatypes and formatting information, to automate the use of the other formatting classes, like <tt>DateFormat</tt> and <tt>NumberFormat</tt>. For instance, to specify the second parameter should be output as a long-formatted date string, you would use <tt>{1,date,long}</tt> as the argument. The <tt><a href="http://java.sun.com/products/jdk/1.2/docs/api/java/text/MessageFormat.html">MessageFormat</a></tt> API documentation provides complete information on all the special formatting options.</p><H2><a name="locAConcl">Starting Application</a></H2>
<p>So, now that the program displays localized text labels and localized date/number formats, you can consider it a <i>global program</i>. It has been localized to adapt to the locale of the user and internationalized by isolating the language dependent pieces. Localizing without internationalizing could result in 3 separate programs, one specific to each area.</p><p>With the help of some resource bundles, the sample program is now complete.</p><center>
<table>
<tr><td>Finland<br><img src="images/class-fi.gif"/>
</td></tr>
<tr><td>Italy<br><img src="images/class-it.gif"/>
</td></tr>
</table>
</center><ul>
<li>training-labels-bundle [<a href="src/training-labels-bundle.java">US</a> |
<a href="src/training-labels-bundle-fi.java">FI</a> |
<a href="src/training-labels-bundle-it.java">IT</a>]</li>
<li>training-settings-bundle [<a href="classes/training-settings-bundle.properties">US</a> |
<a href="classes/training-settings-bundle-fi.properties">FI</a> |
<a href="classes/training-settings-bundle-it.properties">IT</a>]</li>
</ul><p>To run an application with a different locale, you would either programmatically change the locale or configure the <tt>System</tt> properties of <tt>user.language</tt> and <tt>user.region</tt>. For instance, the following starts the <tt>ClassSchedule</tt> program with the Italian locale:</p><pre>
java -Duser.language=it -Duser.region=IT ClassSchedule
</pre><H2><a name="locText">Displaying Unicode Characters</a></H2>
<p>After following Sun's instructions for <a href="internationalization.html#locOnline">adding fonts to the Java runtime</a>, you can now have your programs display non-Western/Latin1 characters. While one would think this would work for <tt>TextField</tt> and <tt>TextArea</tt> components, it really only works when drawing strings or with some AWT components, like <tt>Label</tt> or <tt>Choice</tt>, where user input methods aren't involved. Since Swing components are all drawn from within the Java runtime, this isn't so much of an issue there. The following is a simple demonstration of displaying Japanese characters:</p><center><img src="images/my-display.gif"/>
</center><pre>
import java.awt.*;
import java.awt.event.*;
public class my-display extends Frame {
public static class MyCanvas extends Canvas {
public MyCanvas() {
setFont (new Font ("Serif", Font.PLAIN, 36));
}
public void paint (Graphics g) {
String s = "\u3041\u3042\u3043\u3044\u3045";
g.drawString(s, 20, 50);
}
}
public my-display() {
super("Unicode Tester");
add(new MyCanvas(), BorderLayout.CENTER);
Choice c = new Choice();
c.addItem ("\u3041\u3042\u3043\u3044\u3045");
c.addItem ("\u3041\u3042\u3043\u3044\u3046");
add (c, BorderLayout.SOUTH);
}
public static void main(String[] args) {
Frame f = new my-display();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize (300, 150);
f.setVisible(true);
}
}
</pre><p>There is nothing that prevents you from including non-ASCII characters in resource bundles.</p><H2><a name="locOnline">Online Resources</a></H2>
<p>
<ul>
<li><a href="http://java.sun.com/products/jdk/1.3/docs/guide/intl/index.html">JDK 1.3 Internationalization Information</a></li>
<li><a href="http://java.sun.com/products/jdk/1.3/docs/guide/intl/fontprop.html">Adding Fonts to the Java Runtime</a></li>
<li><a href="http://java.sun.com/products/jilkit/">Java Internationalization and Localization Toolkit</a></li>
<li><a href="http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt">ISO-639, two letter language codes</a></li>
<li><a href="http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html">ISO-3166, two letter country codes</a></li>
</ul>
</p><p><font size=-2>[<i>MML: 2.0b27</i>]</font><br>
<font size=-2>[<i>Version: $ /Internationalization2/internationalization.mml#4 $</i>]</font></p>
<!-- END CONTENT -->
</td>
</tr>
<tr><td colspan=2> </td></tr>
<tr><td bgcolor="#336699" colspan=2><img src="images/footer.gif" width=600 height=9 alt="" border="0"></td></tr>
<tr><td colspan=2><font FACE="verdana,arial,helvetica" SIZE="1">Copyright 1996-2000 jGuru.com</font></td></tr>
</table>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -