JAvaローカライズ

5528 ワード

一、国際化:i 18 N(国際化された単語の略称iからNで終わる18ビットの英語)を略し、開発したプログラムを世界の異なる国や地域の過程に適応させる.
二、ローカライズ:ある地域や言語の使用習慣にプログラムを適応させる過程
1、ローカライズの目標:数値、通貨、日付、イベント、テキスト
2、ローカライズの実現:
1)数値と通貨をローカライズするには:
a.Localeクラスを使用してローカライズする言語を定義する:
Locale aLocale=new Locale("zh","CN");
b.数字のフォーマット:
NumberFormat numberFormat=NumberFormat.getNumberInstance(aLocale);//パラメータは確定した国を表します
c.ローカルの数字のフォーマットを使用して、目標数字をフォーマットする:
String result=numberFormat.format(number);
使用法参照コード:
public class NumberAnCurrentcy {
	public static void main(String[] args) {
		//         ,         ,      
		Locale aLocale=new Locale("zh","CN");
		Locale bLocale=new Locale("ru","RU");//  
		Locale cLocale=Locale.FRANCE;//  

		//   int    
		Integer number=new Integer(123456);//   int i=123456
		
		NumberAnCurrentcy n=new NumberAnCurrentcy();
		n.formatNumber(aLocale, number);
		n.formatNumber(bLocale, number);
		n.formatNumber(cLocale, number);
		
		n.formatCurrentcy(aLocale, number);
		n.formatCurrentcy(bLocale, number);
		n.formatCurrentcy(cLocale, number);
	}
	//     
	private  void formatNumber(Locale aLocale, Integer number) {
		//NumberFormat       ,           ,              
		NumberFormat numberFormat=NumberFormat.getNumberInstance(aLocale);//         
		//                  
		String result=numberFormat.format(number);
		System.out.println(result);
	}
	//     
	private  void formatCurrentcy(Locale aLocale, Integer number) {
		NumberFormat numberFormat=NumberFormat.getCurrencyInstance(aLocale);//         
		String result=numberFormat.format(number);
		
		Currency c=Currency.getInstance(aLocale);//         
		System.out.println(c.getDisplayName()+":"+result);//getDisplayName         
	}
}

2)ローカライズされた日付と時刻を実現するには、ローカライズされた数値と通貨を実現するのと同じように、使用法の参照コードを使用します.
public class DataAndTime {
	public static void main(String[] args) {
		//          ,         ,    ,
		Locale aLocale = new Locale("zh", "CN");
		Locale bLocale = new Locale("ru", "RU");//   
		Locale cLocale = Locale.FRANCE;//   
		DataAndTime d=new DataAndTime();
                d.formatData(aLocale);
                d.formatData(bLocale);
                d.formatData(cLocale);
        
                d.formatTime(aLocale);
                d.formatTime(bLocale);
                d.formatTime(cLocale);
	}
	public void formatData(Locale aLocale) {
		//                  
		//     :        DEFAULT LONG SHORT MEDIUM
		//               
		DateFormat dateFormat=DateFormat.getDateInstance(
				DateFormat.LONG,aLocale);
		//                   
		System.out.println(dateFormat.format(new Date()));//new Date()      
	}
    //     :   
	public void formatTime(Locale aLocale) {
		//                  
		//     :        DEFAULT LONG SHORT MEDIUM
		//               
		DateFormat Date=DateFormat.getTimeInstance(
				DateFormat.LONG,aLocale);
		//                   
		System.out.println(Date.format(new Date()));
	}
}

3)ローカライズされたテキストステップを実現する:
a.異なる国と地域の文字をリソースファイルに保存する(**.properties)方法:対応する項目のsrcにnew--file---ファイル名を書く(ファイル名規則:MessageBundle_言語コード_国家コード.properties、例えば中国語:MessageBundle_zh_CN.properties)
b.Localeに従って対応するファイルを読み込み、画面に表示する
使用法参照コード:
public class FormatTextDemo extends JFrame implements ActionListener{
	JLabel name,password,language;
	JComboBox selectLanguage;
	String strName,strPassWord,strLanguage;
	public FormatTextDemo(){
		setVisible(true);
		setSize(300,300);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		getContentPane().setLayout(null);
		String[] languages={"  ","English"};
		selectLanguage =new JComboBox(languages);
		selectLanguage.setBounds(230, 50, 100, 40);
		getContentPane().add(selectLanguage);
		
		selectLanguage.addActionListener(this);
		
		language=new JLabel("     :");
		language.setBounds(100, 50, 100, 40);
		name=new JLabel("   ");
		name.setBounds(100, 100, 100, 40);
        password=new JLabel("  ");
        password.setBounds(100, 150, 100, 40);
        getContentPane().add(language);
        getContentPane().add(name);
        getContentPane().add(password);		
	}
	public static void main(String[] args) {
		new FormatTextDemo();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		//          
		if(e.getSource().equals(selectLanguage)){
			//       
			JComboBox jcb= (JComboBox)e.getSource();
			int index=jcb.getSelectedIndex();//         
			switch(index){
			case 0://  
				setText(Locale.US);
				break;
			case 1://  
				setText(Locale.CHINA);
				break;
			default://  
				setText(Locale.CHINA);
				break;
			}			
		}		
	}
    //  Locale       
	public void setText(Locale loacle) {
		//  locale      
		//1   //                   //          
		ResourceBundle message=ResourceBundle.getBundle("MessageBundle",loacle);
		//2      
		strName = message.getString("name");
		strPassWord = message.getString("password");
		strLanguage = message.getString("language");
		//        
		name.setText(strName);
		password.setText(strPassWord);
		language.setText(strLanguage);        
	}
}

そのうち:MessageBundle_zh_CN.propertiesファイルの内容
#key=value
name=user name:
password=password
language=please select language

MessageBundle_en_US.propertiesファイルの内容
#key=value
name=\u7528\u6237\u540D
password=\u5BC6\u7801
language=\u8BF7\u9009\u62E9\u8BED\u8A00