Java程序的国际化

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?
可以看出前边是键值,后面为字符串 调用时便可以这样
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 主要代码为: 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值