您的位置:首页 > 编程语言 > Java开发

Java程序的国际化

2004-10-20 19:52 363 查看
.codediv{border:1px solid gray;background-color:#ffffe1;margin-left:20px;padding:10px;}

java 支持多种语言资源而不需要修改程序,只要在程序外写入配置文件即可以
下面是一个区分不同语言而显示不同内容的例子
首先在程序外新建几个不同国家的资源文件的例子如
MessagesBundle.properties
MessagesBundle_de_DE.properties
MessagesBundle_en_US.properties
MessagesBundle_fr_FR.properties
MessagesBundle_en_US.properties的内容为:
greetings = Hello.
farewell = Goodbye.
inquiry = How are you?

MessagesBundle_fr_FR.properties的内容为:
greetings = Bonjour.
farewell = Au revoir.
inquiry = Comment allez-vous?

可以看出前边是键值,后面为字符串
调用时便可以这样

[code]
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(messages.getString("inquiry"));
System.out.println(messages.getString("farewell"));
}
}


显示结果为
Hello.
How are you?
Goodbye.

同样可以为数字或是货币表示本地化
这是一个在 Java-tutorial.chm 上的一个例子
显示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 without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please 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 IMPLIED 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, MODIFYING 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 < locales.length; i++) {
System.out.println();
displayNumber(locales[i]);
displayCurrency(locales[i]);
displayPercent(locales[i]);
}
}
}

主要代码为:
Double amount = new Double(345987.246);
NumberFormat numberFormatter;
String amountOut;
numberFormatter = NumberFormat.getNumberInstance(currentLocale);
amountOut = numberFormatter.format(amount);
System.out.println(amountOut + " " + currentLocale.toString());
输出为:
345 987,246 fr_FR
345.987,246 de_DE
345,987.246 en_US

也可以自定义输出样式:

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.setGroupingSeparator('^');

String strange = "#,##0.###";
DecimalFormat weirdFormatter =
new DecimalFormat(strange, unusualSymbols);
weirdFormatter.setGroupingSize(4);

String bizarre = weirdFormatter.format(12345.678);
System.out.println(bizarre);
}
}

定义匹配规则在 http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormatSymbols.html

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: