十三、国际化

十三、国际化
    国际化包括 ①数字、货币、百分比国际化 
                      ②日期、时间国际化
                      ③文字国际化(通过文件实现)
    Locale 设置国家/语言环境类(核心类)
    NumberFormat 数字、货币、百分比国际化类
    DateFormat 日期、时间国际化类
    ResourceBundle 文字国际化类(读取properties文件对象)
    步骤:
            1.初始化Locale对象,获取国家/语言环境
            2.实例化并设定上面对应的类(NumberFormat 、DateFormat、ResourceBundle )
            3.输入对应的信息即可
    工具类:
package net.itaem.international;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * 
 * 国际化工具类
 * @author sen
 * @version 1.0,2014年8月29日
 */
public class InternationalUtils {
	
	//国家/语言环境
	private Locale locale ;
	//数字格式
	private NumberFormat numFormat ;
	//日期时间格式
	private DateFormat dateFormat ;

	
	/**
	 * 默认构造器
	 */
	public InternationalUtils(){
		//获取系统默认的国家/语言环境
		this.locale = Locale.getDefault() ;
	}
	
	/**
	 * 参数构造器
	 * @param language
	 * @param country
	 */
	public InternationalUtils(String language,String country){
		//获得指定的国家/语言环境
		this.locale = new Locale(language, country) ;
	}
	
	public void change(String language,String country){
		//获得指定的国家/语言环境
		this.locale = new Locale(language, country) ;
	}
	
	/**
	 * 
	 * 1.信息国际化 (获取properties文件对应值)
	 * @author sen
	 * @param tagName
	 * @return
	 */
	public String formatMsg(String tagName){
		//获取系统默认的国家/语言环境
		//Locale locale3 = new Locale("en", "US"); //英语
		/*
		 * 读取资源文件(properties)对象
		 * properties命名规则    自定义_语言代码_国家代码.properties
		 * 				          找不到资源文件时,默认资源文件为  自定义.properties
		 */
        ResourceBundle bundle = ResourceBundle.getBundle("resourse", locale); 
        //返回对应值
        return bundle.getString(tagName) ; 
	}
	
	/**
	 * 
	 * 2.数字国际化
	 * @author sen
	 * @param digit 数字
	 * @param pattern 模式
	 * 		  pattern=1  为数字模式
	 * 		  pattern=2  为货币模式
	 * 		  pattern=3  为百分比模式
	 * 		  pattern=其他 为数字模式
	 * @return
	 */
	public String formatDigit(Double digit,int pattern){
		
		if(numFormat==null){
			initNumberFormat(pattern) ;
		}
		
		return numFormat.format(digit) ;
	}
	public String formatDigit(String digitStr,int pattern) throws ParseException{
		
		if(numFormat==null){
			initNumberFormat(pattern) ;
		}
		
		return numFormat.parse(digitStr).toString() ;
	}
	
	/**
	 * 
	 * 3.日期国际化
	 * @author sen
	 * @param date 日期
	 * @param pattern 模式
	 * 		  pattern=1  为短格式 14-8-29 21:07:10
	 * 		  pattern=2  为中格式 2014-8-29 21:06:55
	 * 		  pattern=3  为长格式 2014年8月29日 21:07:27
	 * 		  pattern=4  为全格式 2014年8月29日 星期五 21:07:35
	 * 		  pattern=其他 为中格式
	 * @return
	 */
	public String formatDate(Date date,int pattern){
		if(dateFormat==null){
			initDateFormat(pattern);
		}
		
		return dateFormat.format(date) ;
	}
	public String formatDate(String dateStr,int pattern) throws ParseException{
		if(dateFormat==null){
			initDateFormat(pattern);
		}
		
		return dateFormat.parse(dateStr).toString() ;
	}
	
	
	/**
	 * 
	 * 数字格式器初始化
	 * @author sen
	 * @param pattern 模式
	 * 		  pattern=1  为数字模式
	 * 		  pattern=2  为货币模式
	 * 		  pattern=3  为百分比模式
	 * 		  pattern=其他 为数字模式
	 */
	private void initNumberFormat(int pattern){
		switch(pattern){
			case 1:
				numFormat = NumberFormat.getNumberInstance(locale) ;
				break ;
			case 2:
				numFormat = NumberFormat.getCurrencyInstance(locale) ;
				break ;
			case 3:
				numFormat = NumberFormat.getPercentInstance(locale) ;
				break ;
			default:
				numFormat = NumberFormat.getNumberInstance(locale) ;
		}
		//设置小数最大位数
		//numFormat.setMaximumFractionDigits(2) ;
	}
	
	/**
	 * 
	 * 日期格式器初始化
	 * @author sen
	 * @param pattern 模式
	 * 		  pattern=1  为短格式 14-8-29 21:07:10
	 * 		  pattern=2  为中格式 2014-8-29 21:06:55
	 * 		  pattern=3  为长格式 2014年8月29日 21:07:27
	 * 		  pattern=4  为全格式 2014年8月29日 星期五 21:07:35
	 * 		  pattern=其他 为中格式
	 */
	private void initDateFormat(int pattern){
		switch(pattern){
			case 1:
				dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM,locale) ;
				break ;
			case 2:
				dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,locale) ;
				break ;
			case 3:
				dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.MEDIUM,locale) ;
				break ;
			case 4:
				dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.MEDIUM,locale) ;
				break ;
			default:
				dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,locale) ;
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值