软件的国际化:软件开发时,要使它能同时应对世界不一样地区和国家的访问,并针对不一样地区和国家的访问,提供相应的、符合来访者阅读习惯的页面或数据。
国际化(internationalization)又称为i18n(读法为i18n,听说是由于internationalization(国际化)这个单词从i到n之间有18个英文字母,i18n的名字由此而来)html
软件实现国际化,需具有如下两个特征:java
对于软件中的菜单栏、导航条、错误提示信息,状态信息等这些固定不变的文本信息,能够把它们写在一个properties文件中,并根据不一样的国家编写不一样的properties文件。这一组properties文件称之为一个资源包。编程
一个资源包中的每一个资源文件都必须拥有共同的基名。除了基名,每一个资源文件的名称中还必须有标识其本地信息的附加部分。例如:一个资源包的基名是“MessageResource”,则与中文、英文环境相对应的资源文件名则为: “MessageResource_zh.properties” “MessageResource_en.properties”
每一个资源包都应有一个默认资源文件,这个文件不带有标识本地信息的附加部分。若ResourceBundle对象在资源包中找不到与用户匹配的资源文件,它将选择该资源包中与用户最相近的资源文件,若是再找不到,则使用默认资源文件。例如:MessageResource.properties。数组
资源文件的内容一般采用”关键字=值”的形式,软件根据关键字检索值显示在页面上。一个资源包中的全部资源文件的关键字必须相同,值则为相应国家的文字。而且资源文件中采用的是properties格式文件,因此文件中的全部字符都必须是ASCII字码,属性(properties)文件是不能保存中文的,对于像中文这样的非ACSII字符,须先进行编码。
例如:
国际化的中文环境的properties文件:
国际化的英文环境的properties文件:
java提供了一个native2ascII工具用于将中文字符进行编码处理,native2ascII的用法以下所示: 浏览器
在JavaAPI中提供了一个ResourceBundle类用于描述一个资源包,而且 ResourceBundle类提供了相应的方法getBundle,这个方法能够根据来访者的国家地区自动获取与之对应的资源文件予以显示。
ResourceBundle类提供了一个静态方法getBundle,该方法用于装载资源文件,并建立ResourceBundle实例:安全
Locale currentLocale = Locale.getDefault(); ResourceBundle myResources =ResourceBundle.getBundle(basename, currentLocale);
加载资源文件后, 程序就能够调用ResourceBundle实例对象的getString方法获取指定的资源信息名称所对应的值。jsp
String value = myResources.getString(“key");
范例:根据国家地区自动获取与之对应的资源文件。ide
public class Demo1 { public static void main(String[] args) { // 根据资源包基名和语言环境加载对应的语言资源文件 ResourceBundle bundler = ResourceBundle.getBundle("cn.itcast.resource.MessageResource",Locale.CANADA); String username = bundler.getString("username"); String password = bundler.getString("password"); System.out.println(username); System.out.println(password); } }
运行结果: 工具
以下所示:ui
<%@page import="java.util.Locale"%> <%@page import="java.util.ResourceBundle"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>国际化的页面</title> </head> <body> <a href="/day12/login.jsp?language=zh">中文(网页)</a> <a href="/day12/login.jsp?language=en">English(pages)</a> <% String language = request.getParameter("language"); if(language == null || language.equals("")) { language = "zh"; } Locale locale = new Locale(language); // Locale表明当前语言地区和国家 ResourceBundle bundle = ResourceBundle.getBundle("cn.itcast.resource.MessageResource",locale); %> <form action=""> <%=bundle.getString("username") %><input type="text" name="username"><br/> <%=bundle.getString("password") %><input type="password" name="password"><br/> <input type="submit" value="<%=bundle.getString("submit") %>"> </form> </body> </html>
运行结果:
一样一个页面,在不一样语言环境的浏览器下显示出了不一样的语言文字效果,这样就实现了固定文本的国际化。
数值,货币,时间,日期等数据因为可能在程序运行时动态产生,因此没法像文字同样简单地将它们从应用程序中分离出来,而是须要特殊处理。Java中提供了解决这些问题的API类(位于java.util包和java.text包中)
Locale实例对象表明一个特定的地理、政治、文化区域。
一个 Locale对象自己不会验证它表明的语言和国家地区信息是否正确,只是向本地敏感的类提供国家地区信息,与国际化相关的格式化和解析任务由本地敏感的类去完成。(若JDK中的某个类在运行时须要根据Locale对象来调整其功能,这个类就称为本地敏感类)
DateFormat类能够将一个日期/时间对象格式化为表示某个国家地区的日期/时间字符串。
DateFormat类除了可按国家地区格式化输出日期外,它还定义了一些用于描述日期/时间的显示模式的int型的常量,包括FULL, LONG, MEDIUM, DEFAULT, SHORT,实例化DateFormat对象时,可使用这些常量,控制日期/时间的显示长度。
例如,对于日期/时间“2005年9月15日下午4时41分20秒”,如设置了DateFormat的显示模式,则日期/时间显示样式分别为:
实例化DateFormat类有九种方式,如下三种为带参形式,下面列出的三种方式也能够分别不带参,或只带显示样式的参数。
DateFormat对象一般不是线程安全的,每一个线程都应该建立本身的 DateFormat实例对象。
使用DateFormat格式化输出日期。
public class Demo2 { /* * 使用DateFormat格式化输出日期 */ public static void main(String[] args) { Date date = new Date(); // DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINA); // 16-8-8 // DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA); // 2016-8-8 // DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINA); // 2016年8月8日 // DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINA); // 2016年8月8日 星期一 DateFormat df = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CHINA); // 2016-8-8 String str = df.format(date); System.out.println(str); df = DateFormat.getTimeInstance(DateFormat.LONG, Locale.CHINA); System.out.println(df.format(date)); df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, Locale.CHINA); System.out.println(df.format(date)); // 获得默认的DateFormat df = DateFormat.getInstance(); System.out.println(df.format(date)); } }
使用DateFormat反向把一个字符串格式化成一个日期对象。
public class Demo3 { /* * 使用DateFormat反向把一个字符串格式化成一个日期对象 */ public static void main(String[] args) throws ParseException { String str = "2016年8月8日 星期一 下午5:57"; DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, Locale.CHINA); Date date = df.parse(str); System.out.println(date); } }
运行结果为:Mon Aug 08 17:57:00 CST 2016
注意:把相应地区的时间日期转化成date对象时,解析的时间或日期要符合指定的国家、地区格式,不然会抛异常。以下面所示的代码就会抛异常:
public class Demo3 { /* * 使用DateFormat反向把一个字符串格式化成一个日期对象 */ public static void main(String[] args) throws ParseException { String str = "2016年8月8日 星期一 下午5:57"; DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.CHINA); Date date = df.parse(str); System.out.println(date); } }
会抛异常以下:
Exception in thread "main" java.text.ParseException: Unparseable date: "2016年8月8日 星期一 下午5:57"
NumberFormat类能够将一个数值格式化为符合某个国家地区习惯的数值字符串,也能够将符合某个国家地区习惯的数值字符串解析为对应的数值。
NumberFormat类的方法:
实例化NumberFormat类时,可使用locale对象做为参数,也能够不使用,下面列出的是使用参数的。
范例:
public class Demo4 { public static void main(String[] args) throws ParseException { int price = 18; // $18 ¥18 // NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA); // ¥18.00 // NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); // $18.00 // NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.JAPAN); // ¥18 NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.FRANCE); // 18,00 € System.out.println(nf.format(price)); String s = "18,00 €"; nf = NumberFormat.getCurrencyInstance(Locale.FRANCE); System.out.println(nf.parse(s).doubleValue()); // 18.0 double d = 0.1; nf = NumberFormat.getPercentInstance(); System.out.println(nf.format(d)); // 10% } }
若是一个字符串中包含了多个与国际化相关的数据,可使用MessageFormat类对这些数据进行批量处理。
例如:
At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage.
以上字符串中包含了时间、数字、货币等多个与国际化相关的数据,对于这种字符串,可使用MessageFormat类对其国际化相关的数据进行批量处理。
MessageFormat类如何进行批量处理呢?
例如:
At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage.
该语句对应的模式字符串即为:
At {0} on {1},a destroyed {2} houses and caused {3} of damage.
字符串中的{0}、{1}、{2}、{3}就是占位符。
At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage.
该语句对应的模式字符串还可编写为:
On {0}, a hurricance destroyed {1} houses and caused {2} of damage.
如今来格式化以上模式字符串。
范例:
public class Demo5 { public static void main(String[] args) { // 模式字符串 String pattern = "On {0}, a hurricance destroyed {1} houses and caused {2} of damage."; // 实例化MessageFormat对象,并装载相应的模式字符串 MessageFormat format = new MessageFormat(pattern, Locale.CHINA); Object[] params = {new Date(), 99, 1000000}; // 格式化模式字符串,参数数组中指定占位符相应的替换对象 String message = format.format(params); System.out.println(message); } }
运行结果:On 16-8-18 上午9:01, a hurricance destroyed 99 houses and caused 1,000,000 of damage.
还能够将模式字符串编写在资源文件中,以下:
代码以下:
public class Demo6 { public static void main(String[] args) { ResourceBundle bundle = ResourceBundle.getBundle("cn.itcast.resource.MessageResource", Locale.CHINA); String pattern = bundle.getString("message"); MessageFormat format = new MessageFormat(pattern, Locale.CHINA); Object[] params = {new Date(), 99, 100000000}; String message = format.format(params); System.out.println(message); } }
运行结果为:在16-8-18 上午9:16,一场大风摧毁了99所房子,致使了100,000,000钱的损失!!!
占位符有三种方式书写方式:
范例:
public class Demo6 { public static void main(String[] args) { String pattern = "At {0, time, short} on {0, date}, a destroyed {1} houses and caused {2, number, currency} of damage."; MessageFormat format = new MessageFormat(pattern, Locale.US); Object[] params = {new Date(), 99, 100000000}; String message = format.format(params); System.out.println(message); } }
运行结果为:At 9:06 AM on Aug 18, 2016, a destroyed 99 houses and caused $100,000,000.00 of damage. 以上就是JavaWeb开发中国际化的总结内容。