JavaWeb之国际化

(1)Locale类

package zh.i18n.demo;

import java.util.Locale;

public class LocaleDemo {

	public static void main(String[] args) {

		// 使用静态方法创建Locale对象
		Locale locale = Locale.CHINA;
		//Locale locale = new Locale("zh", "CN");

		// 返回此语言环境的语言代码
		System.out.println(locale.getLanguage());// zh
		
		// 返回此语言环境的国家/地区代码
		System.out.println(locale.getCountry());// CN
		
		// 返回适合向用户显示的语言环境语言名
		System.out.println(locale.getDisplayLanguage());// 中文
		
		// 返回适合向用户显示的语言环境国家/地区名
		System.out.println(locale.getDisplayCountry());// 中国
		
		// 返回适合向用户显示的语言环境名。
		System.out.println(locale.getDisplayName());// 中文 (中国)

	}

}

(2)ResourceBundle类

资源包由不同的资源文件组成。在src下创建如下资源文件:

myResource.properties:默认资源文件,其中myResource为资源文件的基名。

myResource_zh_CN.properties或者myResource_zh.properties:中文资源文件,其中myResource为资源文件的基名。

myResource_en_US_properties或者myResource_en_properties:英文资源文件,其中myResource为资源文件的基名。

【注意】:不同国家可能使用同一语言,myResource_en_US_properties特指美国,myResource_en_properties则表示所有使用英语的国家。

myResource.properties 与 myResource_zh_CN.properties 内容一样


myResource_en_US_properties


ResourceBundleDemo.java

package zh.i18n.demo;

import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleDemo {

	public static void main(String[] args) {

		// 资源文件的基名
		String baseName = "myResource";

		// 根据基名,加载 默认资源文件mySource.properties
		ResourceBundle defaultBundle = ResourceBundle.getBundle(baseName);
		System.out.println("username: " + defaultBundle.getString("username"));// username: 小小的太阳
		System.out.println("sex: " + defaultBundle.getString("sex"));// sex: 男

		// 根据基名和Locale,加载 中文资源文件myResource_zh_CN.properties
		ResourceBundle zhBundle = ResourceBundle.getBundle(baseName, Locale.CHINA);
		System.out.println("username: " + zhBundle.getString("username"));//username: 小小的太阳
		System.out.println("sex: " + zhBundle.getString("sex"));// sex: 男

		// 根据基名和Locale,加载 英文资源文件myResource_en_US.properties
		ResourceBundle enBundle = ResourceBundle.getBundle(baseName, Locale.US);
		System.out.println("username: " + enBundle.getString("username"));// username: xxdty
		System.out.println("sex: " + enBundle.getString("sex"));// sex: man

	}

}

(3)DateFormat日期格式化

package zh.i18n.demo;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateFormatDemo {

	public static void main(String[] args) throws Exception {
		
		Date date = new Date();
		
		// 根据 日期/时间的显示模式和locale对象,获取DateFormat对象,只处理日期部分
		DateFormat dateInstance = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA);
		// 日期-->字符串
		String format1 = dateInstance.format(date);
		System.out.println(format1);
		// 字符串-->日期
		Date parse1 = dateInstance.parse(format1);
		System.out.println(parse1);
		
		// 根据 日期/时间的显示模式和locale对象,获取DateFormat对象,处理日期和时间部分
		DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM ,Locale.CHINA );
		// 日期-->字符串
		String format2 = dateTimeInstance.format(date);
		System.out.println(format2);
		// 字符串-->日期
		Date parse2 = dateTimeInstance.parse(format2);
		System.out.println(parse2);
	
		System.out.println("--------------------DateFormat不灵活---------- --------------------");
		
		// 使用SimpleDateFormat
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 日期-->字符串
		String dateString1 = sdf1.format(date);
		System.out.println(dateString1);
		// 字符串-->日期
		Date stringDate1 = sdf1.parse(dateString1);
		System.out.println(stringDate1);
		
		System.out.println("------------------------------------------------------------------");
		
		SimpleDateFormat sdf2 = new SimpleDateFormat("今天是yyyy年MM月dd日 HH时mm分ss秒");
		// 日期-->字符串
		String dateString2 = sdf2.format(date);
		System.out.println(dateString2);
		
		// 报错了。因为"此时是2018年08月08日 08时08分08秒"与"今天是yyyy年MM月dd日 HH时mm分ss秒"不匹配
		//String source2 = "此时是2018年08月08日 08时08分08秒";
		//Date stringDate2 = sdf2.parse(source2);
		//System.out.println(stringDate2);
		
		// 字符串-->日期
		// "此时"改为"今天"
		String source2 = "今天是2018年08月08日 08时08分08秒";
		Date stringDate2 = sdf2.parse(source2);
		System.out.println(stringDate2);
	}
}

(4)NumberFormat数值格式化

package zh.i18n.demo;

import java.text.NumberFormat;
import java.util.Locale;

public class NumberFormateDemo {

	public static void main(String[] args) throws Exception {

		NumberFormat numberFormat = NumberFormat.getInstance();
		// 格式化
		String format1 = numberFormat.format(12.345);
		System.out.println(format1);
		// 解析
		Number parse1 = numberFormat.parse(format1);
		System.out.println(parse1);
		System.out.println("--------------------------------------");

		// 处理整数
		NumberFormat integerFormat = NumberFormat.getIntegerInstance();
		// 格式化
		String format2 = integerFormat.format(12.345);
		System.out.println(format2);
		// 解析
		Number parse2 = integerFormat.parse(format2);
		System.out.println(parse2);
		System.out.println("--------------------------------------");

		// 处理货币
		NumberFormat currencyFormat31 = NumberFormat.getCurrencyInstance(Locale.CHINA);
		// 格式化
		String format31 = currencyFormat31.format(12.345);
		System.out.println(format31);
		// 解析
		Number parse31 = currencyFormat31.parse(format31);
		System.out.println(parse31);
		System.out.println("--------------------------------------");

		// 处理货币
		NumberFormat currencyFormat32 = NumberFormat.getCurrencyInstance(Locale.US);
		// 格式化
		String format32 = currencyFormat32.format(12.345);
		System.out.println(format32);
		// 解析
		Number parse32 = currencyFormat32.parse(format32);
		System.out.println(parse32);
		System.out.println("--------------------------------------");

		// 处理百分数
		NumberFormat percentFormat41 = NumberFormat.getPercentInstance(Locale.CHINA);
		// 格式化
		String format41 = percentFormat41.format(1.2345);
		System.out.println(format41);
		// 解析
		Number parse41 = percentFormat41.parse(format41);
		System.out.println(parse41);
		System.out.println("--------------------------------------");

		// 处理百分数
		NumberFormat percentFormat42 = NumberFormat.getPercentInstance(Locale.US);
		// 格式化
		String format42 = percentFormat42.format(1.2345);
		System.out.println(format42);
		// 解析
		Number parse42 = percentFormat42.parse(format42);
		System.out.println(parse42);

	}

}

(5)MessageFormat字符串格式化

package zh.i18n.demo;

import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;

public class MessageFormatDemo {

	public static void main(String[] args) {

/*		
 		占位符三种形式:
		(1)[argumentIndex]
		(2)[argumentIndex, number|date|time|choice]
		(3)[argumentIndex, number|date|time|choice, integer|currency|percent等]
*/
		
		// 模式字符串,包含占位符
		String patternString1 = "On {0}, {1} {2} maked {3} yuan";
		MessageFormat messageFormat1 = new MessageFormat(patternString1,Locale.CHINA);
		Object[] array1 = new Object[] { new Date(), 100, "students", "123.45" };
		String format1 = messageFormat1.format(array1);
		System.out.println(format1);
		// On 18-6-12 下午3:25, 100 students maked 123.45 yuan
		System.out.println("---------------------------------------------------");
		
		// 模式字符串,包含占位符
		String patternString2 = "On {0,date} at {0,time}, {1,number,integer} {2} maked {3,number,currency}";
		MessageFormat messageFormat2 = new MessageFormat(patternString2,Locale.CHINA);
		Object[] array2 = new Object[] { new Date(), 100, "students", 123.45 };
		String format2 = messageFormat2.format(array2);
		System.out.println(format2);
		// On 2018-6-12 at 15:25:29, 100 students maked ¥123.45

	}

}

国际化-小案例

三个资源文件




i18n.jsp

<%@page import="java.text.MessageFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*"%>
<!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>
	<%
		String baseName = "myResource";/* 资源文件的基名 */
		Locale locale = request.getLocale();/* 获取客户端的语言首选项 */
		ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale);
	 %>
	
	姓名:<%=bundle.getString("username") %><br>
	性别:<%=bundle.getString("sex") %><br>
	<%
		String message = bundle.getString("message");
		MessageFormat messageFormat = new MessageFormat(message,Locale.CHINA);// 首选项为英文时,改为Locale.US
		Object[] array = new Object[]{new Date(), 100, "students", 1234.56};
		String information = messageFormat.format(array);
	 %>
	留言:<%=information %>

</body>
</html>

如果首选项为中文,访问:http://localhost:8080/JavaWeb1/i18n.jsp


如果首选项为英文,访问:http://localhost:8080/JavaWeb1/i18n.jsp


猜你喜欢

转载自blog.csdn.net/qq_41706150/article/details/80661092