_chapter 10.htm

来自「Core Java 2(中文名称:JAVA 2 核心技术 卷二:高级特性)这是英」· HTM 代码 · 共 1,361 行 · 第 1/4 页

HTM
1,361
字号
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Chapter 10</title>
<link rel="stylesheet" type="text/css" href="docsafari.css">
<link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>
<ul></ul>
<table width="100%" border="1" bgcolor="#EBEBFF">
  <tr>
    <td width="5%" align="left" valign="middle"><a href="_chapter%209.htm"><img src="Larrow.gif" width="17" height="19" border="0"></a></td>
    <td align="center" valign="middle"><a class="docLink" href="Front%20matter.htm">CONTENTS</a></td>
    <td width="5%" align="right" valign="middle"><a href="_chapter%2011.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td>
  </tr>
</table>

<h2 class="docChapterTitle">Chapter 10. Internationalization</h2>
<ul>
  <li>
    <p class="docList"><a class="docLink" href="#c10s1">Locales</a></li>
  <li>
    <p class="docList"><a class="docLink" href="#c10s2">Numbers and Currencies</a></li>
  <li> 
    <p class="docList"><a class="docLink" href="#c10s3">Date and Time</a></li>
  <li>
    <p class="docList"><a class="docLink" href="#c10s4">Text</a></li>
  <li> 
    <p class="docList"><a class="docLink" href="#c10s5">Resource Bundles</a></li>
  <li> 
    <p class="docList"><a class="docLink" href="#c10s6">Graphical User Interface Localization</a></li>
</ul>
<p class="docText">There's a big world out there; we hope that lots of its 
inhabitants will be interested in your application or applet. The Internet, 
after all, effortlessly spans the barriers between countries. On the other hand, 
when you write your applet in U.S. English, using the ASCII character set,
<span class="docEmphasis">you</span> are putting up a barrier. For example, even 
within countries that can function using the ASCII character set, things as 
basic as dates and numbers are displayed differently. To a German speaker, the 
date 3/4/95 means something different than it does to an English speaker. Or, an 
applet like our calculator from Chapter 10 of Volume 1 could confuse people who 
do not use the &quot;.&quot; to separate the integer and fractional parts of a number.</p>
<p class="docText">Now, it is true that many Internet users are able to read 
English, but they will certainly be more comfortable with applets or 
applications that are written in their own language and that present data in the 
format with which they are most familiar. Imagine, for example, that you could 
write a retirement calculator applet that would change how it displays its 
results <span class="docEmphasis">depending on the location of the machine that 
is downloading it.</span> This kind of applet is immediately more valuable梐nd 
smart companies will recognize its value.</p>
<p class="docText">The Java programming language was the first language designed 
from the ground up to support internationalization. From the beginning, it had 
the one essential feature needed for effective internationalization: it used 
Unicode for all strings. Unicode support makes it easy to write programs in the 
Java programming language that manipulate strings in any one of multiple 
languages.</p>
<div class="docNote">
  <p class="docNoteTitle">NOTE</p>
  <table cellSpacing="0" cellPadding="1" width="90%" border="0">
    <tr>
      <td vAlign="top" width="60">
      <img alt="graphics/note.gif" src="note.gif" align="left" border="0" width="54" height="53"><br>
&nbsp;</td>
      <td vAlign="top">
      <p class="docText">The best source for Unicode character tables is <i>The 
      Unicode Standard, Version 3.0</i>, [Addison-Wesley<span class="docEmphasis">,</span> 
      2000]. You can also see many of the code charts at
      <a class="docLink" href="http://www.unicode.org" target="_blank">
      http://www.unicode.org</a>.</td>
    </tr>
  </table>
</div>
<p class="docText">Many programmers believe that all they need to do to 
internationalize their application is to support Unicode and to translate the 
messages in the user interface. However, as you will see in this chapter, there 
is a lot more to internationalizing programs than just Unicode support. 
Operating systems and even browsers may not necessarily be Unicode-ready. For 
example, it is almost always necessary to have a translation layer between the 
character sets and fonts of the host machine and the Unicode-centric Java 
virtual machine. Also, dates, times, currencies梕ven numbers梐re formatted 
differently in different parts of the world. You need an easy way to configure 
menu and button names, message strings, and keyboard shortcuts for different 
languages. You need to trigger the changes in a way that is based on information 
the ambient machine can report to your program.</p>
<p class="docText">In this chapter, you'll see how to write internationalized 
Java applications and applets. You will see how to localize date and time, 
numbers and text, and graphical user interfaces, and you'll look at the tools 
that the SDK offers for writing internationalized programs. (And, by the way, 
you will see how to write a retirement calculator applet that can change how it 
displays its results <span class="docEmphasis">depending on the location of the 
machine that is downloading it.</span>)</p>
<h3 class="docSection1Title" id="c10s1">Locales</h3>
<p class="docText">When you look at an application that is adapted to an 
international market, the most obvious difference you notice is the language. 
This observation is actually a bit too limiting for true internationalization: 
Countries can share a common language, but you still may need to do some work to 
make computer users of both countries happy.<span id="ENB10-1"><a class="docLink" href="#EN10-1"><sup>[1]</sup></a></span></p>

<p class="docText">In all cases, menus, button labels, and program messages will 
need to be translated to the local language; they may also need to be rendered 
in a different script. There are many more subtle differences; for example, 
numbers are formatted quite differently in English and in German. The number</p>
<pre>123,456.78
</pre>
<p class="docText">should be displayed as</p>
<pre>123.456,78
</pre>
<p class="docText">for a German user. That is, the role of the decimal point and 
the decimal comma separator are reversed! There are similar variations in the 
display of dates. In the United States, dates are somewhat irrationally 
displayed as month/day/year. Germany uses the more sensible order of 
day/month/year, whereas in China, the usage is year/month/day. Thus, the date</p>
<pre>3/22/61
</pre>
<p class="docText">should be presented as</p>
<pre>22.03.1961
</pre>
<p class="docText">to a German user. Of course, if the month names are written 
out explicitly, then the difference in languages becomes apparent. The English</p>
<pre>March 22, 1961
</pre>
<p class="docText">should be presented as</p>
<pre>22. M鋜z 1961
</pre>
<p class="docText">in German, or</p>
<p>&nbsp;</p>
<center>
<p>
<img alt="graphics/10fig08.gif" src="10fig08.gif" border="0" width="130" height="25"><br>
&nbsp;</p>
</center>
<p class="docText">in Chinese.</p>
<p class="docText">You saw in Volume 1 that the <tt>java.text</tt> class has 
methods that can format numbers, currencies, and dates. These methods can, in 
fact, do much more when you give them a parameter that describes the location. 
To invoke these methods in a non-country-specific way, you only have to supply 
objects of the <tt>Locale</tt> class. A <span class="docEmphasis">locale</span> 
describes</p>
<ul>
  <li>
  <p class="docList">A language;</li>
  <li>
  <p class="docList">A location;</li>
  <li>
  <p class="docList">Optionally, a variant.</li>
</ul>
<p class="docText">For example, in the United States, you use a locale with</p>
<blockquote>
  <p class="docList">language=English, location=United States.</p>
</blockquote>
<p class="docText">In Germany, you use a locale with</p>
<blockquote>
  <p class="docList">language=German, location=Germany.</p>
</blockquote>
<p class="docText">Switzerland has four official languages (German, French, 
Italian, and Rhaeto-Romance). A German speaker in Switzerland would want to use 
a locale with</p>
<blockquote>
  <p class="docList">language=German, location=Switzerland</p>
</blockquote>
<p class="docText">This locale would make formatting work similarly to how it 
would work for the German locale; however, currency values would be expressed in 
Swiss francs, not German marks.</p>
<p class="docText">Variants are, fortunately, rare and are needed only for 
exceptional or system-dependent situations. For example, the Norwegians are 
having a hard time agreeing on the spelling of their language (a derivative of 
Danish). They use two spelling rule sets: a traditional one called Bokm錶 and a 
new one called Nynorsk. The traditional spelling would be expressed as a variant</p>
<blockquote>
  <p class="docList">language=Norwegian, location=Norway, variant=Bokm錶</p>
</blockquote>
<p class="docText">There are &quot;Euro&quot; variants for several European locales. For 
example, the locale</p>
<blockquote>
  <p class="docList">language=German, location=Germany, variant=Euro</p>
</blockquote>
<p class="docText">uses the euro sign $ instead of DM for displaying currencies.</p>
<p class="docText">It is also possible to encode platform-dependent information 
in the variant.</p>
<p class="docText">To express the language and location in a concise and 
standardized manner, the Java programming language uses codes that were defined 
by the International Standards Organization. The local language is expressed as 
a lowercase two-letter code, following ISO-639, and the country code is 
expressed as an uppercase two-letter code, following ISO-3166.
<a class="docLink" href="#ch10table01">Tables 10-1</a> and
<a class="docLink" href="#ch10table02">10-2</a> show some of the most common 
codes.</p>
<div class="docNote">
  <p class="docNoteTitle">NOTE</p>
  <table cellSpacing="0" cellPadding="1" width="90%" border="0">
    <tr>
      <td vAlign="top" width="60">
      <img alt="graphics/note.gif" src="note.gif" align="left" border="0" width="54" height="53"><br>
&nbsp;</td>
      <td vAlign="top">
      <p class="docText">For a full list of ISO-639 codes, see, for example,</p>
      <p class="docText">
      <a class="docLink" href="http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt" target="_blank">
      http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt</a>.</p>
      <p class="docText">You can find a full list of the ISO-3166 codes at a 
      number of sites, including</p>
      <p class="docText">
      <a class="docLink" href="http://www.niso.org/3166.html" target="_blank">
      http://www.niso.org/3166.html</a>.</td>
    </tr>
  </table>
  <p>&nbsp;</div>
<table cellSpacing="0" cellPadding="1" width="100%" border="1">
  <caption>
  <h5 id="ch10table01" class="docTableTitle">Table 10-1. Common ISO-639 language codes</h5>
  </caption>
  <colgroup span="2" align="left">
  </colgroup>
  <tr>
    <th class="docTableHeader" vAlign="center"><span class="docEmphStrong">
    Language</span> </th>
    <th class="docTableHeader" vAlign="center"><span class="docEmphStrong">Code</span>
    </th>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Chinese </td>
    <td class="docTableCell" vAlign="top"><tt>zh</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Danish </td>
    <td class="docTableCell" vAlign="top"><tt>da</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Dutch </td>
    <td class="docTableCell" vAlign="top"><tt>nl</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">English </td>
    <td class="docTableCell" vAlign="top"><tt>en</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">French </td>
    <td class="docTableCell" vAlign="top"><tt>fr</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Finnish </td>
    <td class="docTableCell" vAlign="top"><tt>fi</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">German </td>
    <td class="docTableCell" vAlign="top"><tt>de</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Greek </td>
    <td class="docTableCell" vAlign="top"><tt>el</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Italian </td>
    <td class="docTableCell" vAlign="top"><tt>it</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Japanese </td>
    <td class="docTableCell" vAlign="top"><tt>ja</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Korean </td>
    <td class="docTableCell" vAlign="top"><tt>ko</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Norwegian </td>
    <td class="docTableCell" vAlign="top"><tt>no</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Portuguese </td>
    <td class="docTableCell" vAlign="top"><tt>pt</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Spanish </td>
    <td class="docTableCell" vAlign="top"><tt>sp</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Swedish </td>
    <td class="docTableCell" vAlign="top"><tt>sv</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Turkish </td>
    <td class="docTableCell" vAlign="top"><tt>tr</tt> </td>
  </tr>
</table>
<table cellSpacing="0" cellPadding="1" width="100%" border="1">
  <caption>
  <h5 id="ch10table02" class="docTableTitle">Table 10-2. Common ISO-3166 country codes</h5>
  </caption>
  <colgroup span="2" align="left">
  </colgroup>
  <tr>
    <th class="docTableHeader" vAlign="top"><span class="docEmphStrong">Country</span>
    </th>
    <th class="docTableHeader" vAlign="top"><span class="docEmphStrong">Code</span>
    </th>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Austria </td>
    <td class="docTableCell" vAlign="top"><tt>AT</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Belgium </td>
    <td class="docTableCell" vAlign="top"><tt>BE</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Canada </td>
    <td class="docTableCell" vAlign="top"><tt>CA</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">China </td>
    <td class="docTableCell" vAlign="top"><tt>CN</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Denmark </td>
    <td class="docTableCell" vAlign="top"><tt>DK</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Finland </td>
    <td class="docTableCell" vAlign="top"><tt>FI</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Germany </td>
    <td class="docTableCell" vAlign="top"><tt>DE</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Great Britain </td>
    <td class="docTableCell" vAlign="top"><tt>GB</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Greece </td>
    <td class="docTableCell" vAlign="top"><tt>GR</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Ireland </td>
    <td class="docTableCell" vAlign="top"><tt>IE</tt> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">Italy </td>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?