Java程序的国际化
2004-10-20 19:52
363 查看
.codediv{border:1px solid gray;background-color:#ffffe1;margin-left:20px;padding:10px;}
显示结果为
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]
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]
相关文章推荐
- Java常用类库--国际化程序
- <JAVA学习笔记一>——程序的国际化
- Java复习之程序的国际化
- Java学习之国际化程序
- Java基础系列7:国际化程序简介
- Java中如何实现程序国际化
- java程序国际化
- Java程序的国际化和本地化实例
- java使用资源文件实现程序的国际化
- JAVA国际化程序实现
- Java中的国际化程序
- Java国际化程序
- 【Java常用类库】_国际化程序
- 传智播客java web 学习,国际化的web程序开发
- Java中如何实现程序国际化
- [转]JAVA程序国际化
- 传智播客java web 学习,国际化的web程序开发(二)
- java程序的国际化2
- 【Java】利用Java对配置文件操作实现Java程序的国际化
- java学习之路----java类库---程序国际化