INTERNATIONALIZATION (Internationalization)

xiaoxiao2021-03-06  15

http://java.sun.com/docs/books/tutorial/i18n/

This is a simple sample ..

Import java.util. *;

Public class i18nsample {

Static public void main (string [] args) {

String Language;

String country;

IF (args.length! = 2) {

Language = New String ("en");

Country = New String ("US");

} else {

Language = new string (args [0]);

Country = new string (args [1]);

}

Locale Currentlocale;

ResourceBundle Messages;

Currentlocale = New Locale (Language, Country);

Messages = ResourceBundle.getBundle ("MessageSbundle",

Currentlocale);

System.out.println (Messages.getstring ("Greetings");

System.out.println ("INQUIRY"));

System.out.println (Messages.getstring ("FAREWELL");

}

}

To Compile and Run This Program, you need these Source Files:

I18nsample.java messagesbundle.properties messagesbundle_de_de.properties message message message message messagesbundle_fr_fr.properties

Below is the need to pay attention to internationalization.

Checklist

Many programs are not internationalized when first written These programs may have started as prototypes, or perhaps they were not intended for international distribution If you must internationalize an existing program, take the following steps..:

Identify Culturally Dependent Data

Text Messages Are The Most Obvious Form of Data That Varies with Culture. However, Other Types of Data May Vary with region or language. The Following List Contains Examples of Culturally Dependent Data:

Messages Labels On GUI Components Online Help Sounds Colors Graphics Icons Dates Times Numbers Currencies Measurements Phone NumBers Honorifics and Personal Titles Postal Addresses Page Layouts

Isolate Translatable Text in Resource Bundles

Translation is costly. You can help reduce costs by isolating the text that must be translated inResourceBundle objects. Translatable text includes status messages, error messages, log file entries, and GUI component labels. This text is hardcoded into programs that have not been internationalized .................................... ..

String buttonLabel = "ok";

...

JButton okbutton = new jbutton (ButtonLabel);

See the section isolating local-specific data for details.

DEAL with Compound Messages

Compound messages contain variable data. In the message "The disk contains 1100 files." The integer 1100 may vary. This message is difficult to translate because the position of the integer in the sentence is not the same in all languages. The following message is NOT Translatable, Because The Order of The Sentence Elements IS Hardcode by ConcateNation:

Integer filecount;

...

String diskstatus = "the disk contains" filecount.toString ()

"Files."

Whenever possible, you should avoid constructing compound messages, because they are difficult to translate. However, if your application requires compound messages, you can handle them with the techniques described in the section

Messages.

Format NumBers and currencies

If your application displays numbers and currencies, you must format them in a locale-independent manner The following code is not yet internationalized, because it will not display the number correctly in all countries.:

Double amount;

Textfield Amountfield;

...

String displayamount = amount.toString ();

Amountfield.Settext (DisplayAmount);

You should replace the preceding code with a routine that formats the number correctly. The Java programming language provides several classes that format numbers and currencies. These classes are discussed in the section Numbers and Currencies.Format Dates and Times

Date and Time Formats Differ With Region and Language. If Your Code Contains Statements Like The Following, You NEED To Change IT:

Date currentdate = new date ();

TextField Datefield;

...

String datestring = currentdate.tostring ();

Datefield.Settext (dateString);

.................. ..

Use Unicode Character Properties

The Following Code Tries to Verify That a Character Is A Letter:

CHAR CH;

...

IF ((ch> = 'a' && ch <= 'z') ||

(CH> = 'a' && ch <= 'z')) // WRONG!

Watch Out for Code Like this, Because IT Won't work with languages ​​Other Than English. For example, the

IF Statement Misses The Character ü in The German Word Grün.

The Character Comparison Methods Use the Unicode Standard To Identify Character Properties. Thus you sell replace the previous code with the folload:

CHAR CH;

...

IF (Character.isletter (CH))

For more information on the

Character Comparison Methods, See The Section

Checking Character Properties.

Compare Strings Properly

When Sorting text you offen compare strings. If the text is displayed, you shouth't use the commitness methods of the

String Class. A Program That Hasn't Been Internationalized Might Compare Strings As Follows:

String Target;

String Candidate;

...

IF (target.equals (candidate)) {

...

IF (target.com <0) {

...

The String.equals and String.compareTo methods perform binary comparisons, which are ineffective when sorting in most languages. Instead you should use the Collator class, which is described in the section Comparing Strings.

Convert Non-Unicode Text

Characters in the Java programming language are encoded in Unicode. If your application handles non-Unicode text, you might need to translate it into Unicode. For more information, see the section Converting Non-Unicode Text.

转载请注明原文地址:https://www.9cbs.com/read-46053.html

New Post(0)