Java I18N

博客围绕Java国际化展开,介绍了需国际化的元素,如消息、日期、数字等。给出语言和国家代码的查询地址,展示了获取支持的Locale定义的方法。通过示例代码演示了资源包查找、数字和货币、日期和时间、消息的格式化操作。

- What to be internationalized ?
   Messages
   Labels on GUI
   Online help
   Sounds
   Graphics
   Icons
   Dates
   Times
   Numbers
   Currencies
   Measurements
   Phone Numbers
   Honorifics and personal titles
   Postal address
   Page layouts
 
- Language Code & Country Code
 
http://ftp.ics.uci.edu/pub/ietf/http/related/iso639.txt (Language Code)
 
http://userpage.chemie.fu-berlin.de/diverse/doc/ISO_3166.html (Country Code)
 
- Find out which Locale definitions are supported by the DateFormat class
  Locale list[] = DateFormat.getAvailableLocales();
 
- Example Code
  String languageCode = "en";
  String countryCode = "US";
  Locale currentLocale = new Locale(languageCode, countryCode);
 
  ResourceBundle labels = ResourceBundle.getBundle("LabelsBundle",currentLocale);
  System.out.println(labels.getString("ok"));

 
- The order of ResourceBundle looking up properties file
  If defaule locale is en_US
  Locale currentLocale = new Locale("fr", "CA", "UNIX");
  ResourceBundle introLabels = ResourcBundle.getBundle("ButtonLabel", currentLocale);
  ButtonLable_fr_CA_UNIX
  ButtonLable_fr_CA
  ButtonLable_fr
  ButtonLable_en_US
  ButtonLable_en
  ButtonLable
 
- How to format Numbers and Currencies
  Double amount = new Double(345987.246);
  numberFormatter = NumberFormat.getNumberInstance(currentLocale);
  amountOut = numberFormatter.format(amount);
  ============================================================
  Double currency = new Double(9876543.21);
  currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
  currencyOut = currencyFormatter.format(currency);
  ============================================================ 
  percentFormatter = NumberFormat.getPercentInstance(currentLocale);
  percentOut = percentFormatter.format(percent);
 
- How to format Dates and Times
  dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);
  today = new Date();
  dateOut = dateFormatter.format(today);

  timeFormatter = DateFormat.getTimeInstance(DateFormat.DEFAULT, currentLocale);
  dateTimeFormatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, currentLocale);
 
 
  SimpleDateFormat formatter = new SimpleDateFormat(pattern, currentLocale);
  Date today = new Date();
  String output = formatter.format(today);
 
- How to format Messages
  MessageFormat formatter = new MessageFormat("");
  formatter.setLocale(currentLocale);
  formatter.applyPattern("template = At {2,time,short} on {2,date,long}, we detected {1,number,integer} spaceships on the planet {0}.");
  String output = formatter.format(messageArguments);

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值