Java程序的国际化和本地化实例

package com.jiepu;

import java.util.*;
/**
 * PropertyResourceBundle Example
 * @author Administrator
 *http://zhwj184.iteye.com/blog/1872380
 *http://www.ibm.com/developerworks/cn/java/joy-i18n/index.html
 *Java里面的资源文件叫做ResourceBundle,
 *它分成两种,一种是ListResourceBundle,另一种是PropertyResourceBundle,
 */
public class I18NSample {

	 public  static void main(String[] args) {

		String language;
		String country;

		if (args.length != 2) {
			//language = new String("zh");
			//country = new String("CN");
			language = new String("zh");
			country = new String("TW");
		} else {
			language = new String(args[0]);
			country = new String(args[1]);
		}

		/**for(Locale tmp:Locale.getAvailableLocales())
		{
			System.out.println(tmp);
		}*/
		Locale currentLocale=new Locale(language, country);
		//需要MessagesBundle_zh_CN.properties MessagesBundle_en_US.properties等
		ResourceBundle messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
		
		System.out.println(messages.getString("greetings"));
		System.out.println(messages.getString("inquiry"));
		System.out.println(messages.getString("farewell"));
	}
}
package com.jiepu;

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

/**
 * ListResourceBundle Example
 * http://blog.csdn.net/netpirate/article/details/6076742
 * @author Administrator
 *
 */
public class I18N {

	public static void main(String[] args) {

		String baseName = "com.jiepu.Rb";
		String key = "MSG";
		try {
			ResourceBundle res = ResourceBundle.getBundle(baseName);
			System.out.println(res.getString(key));

			res = ResourceBundle.getBundle(baseName, Locale.ENGLISH);
			System.out.println(res.getString(key));
		} catch (MissingResourceException exp) {
			exp.printStackTrace();
		}

	}
}
package com.jiepu;

import java.util.ListResourceBundle;


public class Rb_en extends ListResourceBundle
{
    static final Object[][] contents = {
            { "CHG", "change" },
            { "MSG", "message" },
            { "BYE", "bye" }
    };

    public Object[][] getContents()
    {
        return contents;
    }
}

package com.jiepu;


import java.util.ListResourceBundle;

public class Rb extends ListResourceBundle
{
    static final Object[][] contents = {
            { "CHG", "修改" }, 
            { "MSG", "消息" }, 
            { "BYE", "再见" }
    };

    public Object[][] getContents()
    {
        return contents;
    }
}

MessagesBundle_zh_CN.properties

greetings = \u4F60\u597D  
farewell = \u518D\u89C1 
inquiry = \u4F60\u8FC7\u5F97\u600E\u4E48\u6837 




你可能感兴趣的:(java)