Internationalization of Java program

xiaoxiao2021-03-06  98

Java supports multiple languages ​​and does not need to modify the program, as long as you write the configuration file outside the program.

Here is an example of distinguishing between different languages

First, an example of a new number of resource files from several different countries outside the program

MessagesBundle.properties

MessagesBundle_de_de.properties

MessagesBundle_en_us.properties

MessagesBundle_fr_fr.properties

MessagesBundle_en_us.properties content is:

Greetings = hello.

Farewell = Goodbye.

INQUIRY = How are you?

MessagesBundle_fr_fr.properties is:

Greetings = Bonjour.

FAREWELL = au revoir.

Inquiry = comment allez-vous?

It can be seen that the front is the key value, followed by a string

It's like this.

Import java.util. *;

Public class test {

Public static void main (String [] args) {

Locale Currentlocale;

ResourceBundle Messages;

CurrentLocale = New Locale ("en", "US");

Messages = ResourceBundle.getBundle ("MessageSbundle",

Currentlocale);

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

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

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

}

}

Display result

Hello.

How are you?

Goodbye.

Localization can also be represented by numbers or currency

This is an example in java-tutorial.chm

Show NumberformATDemo.java

/ *

* Copyright (C) 1995-1998 Sun Microsystems, Inc. All Rights Reserved.

*

* Permission To Use, Copy, Modify, and Distribute this Software

* AND ITS Documentation for non-Commercial Purposes and with welcom

* Fee is hereby granted provided That this Copyright NOTINET NOTE

* APPEASE REFER TO The file "Copyright.html"

* for Further Important Copyright and Licensing Information.

*

* Sun Makes no representations or warranties about the suitability of

* The Software, Either Express Or Implied, Including But Not Limited

* To the import Warranties of Merchantability, Fitness for a

* Particular Purpose, or Non-Infringement. Sun Shall Not Be Liable for

* Any Damages Suffered by licensee as a result of using, modify or * distributing this software or its derivatives.

* /

Import java.util. *;

Import java.text. *;

Public class numberformatdemo {

Static Public Void DisplayNumber (Locale Currentlocale) {

Integer QUANTITY = New Integer (123456);

Double Amount = New Double (345987.246);

Numberformat NumberFormatter;

String quantityout;

String amounTout;

Numberformatter = Numberformat.GetNumberinstance (Currentlocale);

Quantityout = NumberFormatter.Format (Quantity);

AmounTout = Numberformatter.Format (Amount);

System.out.println (Quantityout " CurrentLocale.toString ());

System.out.println (Amountout " CurrentLocale.toString ());

}

Static Public Void DisplayCurrency (Locale Currentlocale) {

Double Currency = New Double (9876543.21);

Numberformat CurrencyFormatter;

String currencyout;

Currencyformatter = Numberformat.getCurrencyInstance (Currentlocale);

Currencyout = currencyformatter.format (currency);

System.out.println (Currencyout " CurrentLocale.toString ());

}

Static Public Void DisplayPercent (Locale Currentlocale) {

Double percent = new double (0.75);

Numberformat percentformatter;

String percentout;

Percentformatter = NumberFormat.getPercentInstance (Currentlocale);

Percentout = percentformatter.format (percent);

System.out.println (Percentout " CurrentLocale.toString ());

}

Static public void main (string [] args) {

Locale [] locales = {

New Locale ("fr", "fr"),

New Locale ("de", "de"),

New Locale ("en", "us")

}

For (int i = 0; i

SYSTEM.OUT.Println ();

DisplayNumber (Locales [i]);

DisplayCurrency (Locales [i]);

DisplayPercent (Locales [i]);

}

}

}

The main code is:

Double Amount = New Double (345987.246);

Numberformat NumberFormatter;

String amounTout;

Numberformatter = Numberformat.GetNumberinstance (Currentlocale);

AmounTout = Numberformatter.Format (Amount);

System.out.println (Amountout " CurrentLocale.toString ());

The output is:

345 987, 246 FR_FR

345.987, 246 de_de

345, 987.246 en_us

You can also customize the output style:

Import java.util. *;

Import java.text. *;

Public class custformat {

Public static void main (String [] args) {

Locale Currentlocale = New Locale ("en", "US");

DecimalFormatsymbols unusualsymbols =

New DecimalFormatsymbols (Currentlocale);

UNUSUALSYMBOLS.SETDECIMALSEPARATOR ('|');

Unusualsymbols.setgroupingseParetor ('^');

String strange = "#, ## 0. ###";

DecimalFormat WeirdFormatter =

New DecimalFormat (Strange, UnusualSymbols);

WeirdFormatter.SetGroupingsize (4);

String bizarre = weirdformatter.Format (12345.678);

System.out.println (Bizarre);

}

}

Define matching rules at http://java.sun.com/J2se/1.4.2/docs/api/java/text/decimalformatsymbols.html

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

New Post(0)