Java国际化(I18N)
计算机编程 有关的国际化是设计和编写应用程序以便可以在全球或多国环境中使用的过程,国际化程序能够支持不同的语言以及不同格式的日期、时间、货币和其它值,而无须软件修改。这通常涉及“软编码”或将文本组件同程序代码分离并且可能涉及可插入代码模块。
Java 提供了一个工具类ResourceBundle,来实现Java的国际化,核心思想是,不同的语言提供一个不同的资源文件。
import java.util.Locale;
public class I18NLocale {
public static void main(String[] args) {
// 返回Java所支持的全部国家和语言的数组
Locale[] localeList = Locale.getAvailableLocales();
// 遍历数组的每个元素,依次获取所支持的国家和语言
for (int i = 0; i < localeList.length; i++) {
// 打印出所支持的国家和语言
System.out.print(localeList[i].getDisplayCountry() + "=" + localeList[i].getCountry() + "\t");
System.out.print(localeList[i].getDisplayLanguage() + "=" + localeList[i].getLanguage());
System.out.println();
}
}
}
国家化案例
国际化文件(com/what21/demo/demo13/Message_en_US.properties)
k1=hello!
k2=good bye!
k3=Can I help you! {0},{1}
国际化文件(com/what21/demo/demo13/Message_zh_CN.properties)
k1=\u4F60\u597D\u5417\uFF1F
k2=\u518D\u89C1\uFF01
k3=\u6211\u80FD\u5E2E\u52A9\u4F60\u5417\uFF1F{0}\uFF0C{1}
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* Java国际化
*/public class I18NDemo {
/**
* 获取计算机默认的locale
*
* @return
*/ public static Locale getDefaultLocale() {
Locale defaultLocale = Locale.getDefault();
String defaultLanguage = defaultLocale.getLanguage();
String defaultCountry = defaultLocale.getCountry();
System.out.println("默认语言:" + defaultLanguage);
System.out.println("默认国家:" + defaultCountry);
return defaultLocale;
}
/**
* 获取构造的Locale
*
* @param language
* @param country
* @return
*/ public static Locale getNewLocale(String language, String country) {
Locale locale = new Locale(language, country);
String newLanguage = locale.getLanguage();
String newCountry = locale.getCountry();
System.out.println("语言:" + newLanguage);
System.out.println("国家:" + newCountry);
return locale;
}
/**
* 输出国际化资源文件
*
* @param fileName
* @param locale
* @param str1
* @param str2
*/ public static void message(String fileName, Locale locale,
String str1, String str2) {
ResourceBundle bundle = ResourceBundle.getBundle(fileName, locale);
System.out.println("k1 = " + bundle.getString("k1"));
System.out.println("k2 = " + bundle.getString("k2"));
// 填充 占位符 (2个占位符)
MessageFormat messageFormat = new MessageFormat(bundle.getString("k3"));
String k3 = messageFormat.format(new Object[]{str1, str2});
System.out.println("k3 = " + k3);
}
/**
* @param args
*/ public static void main(String[] args) {
message("com/what21/demo/demo13/Message", getDefaultLocale(), "汤姆", "小赵");
System.out.println("---------------------------------------------");
message("com/what21/demo/demo13/Message", getNewLocale("en", "US"), "tom", "zhang");
}
}
国际化时间和日期
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class Demo {
public static void main(String[] args) throws Exception {
//模拟语言环境
Locale chinaLocale = Locale.CHINA;
Locale englishLocale = Locale.ENGLISH;
//日期格式&&时间格式
int dataStyle = DateFormat.SHORT;
int timeStyle = DateFormat.SHORT;
DateFormat df1 = DateFormat.getDateTimeInstance(dataStyle, timeStyle, chinaLocale);
String date1 = df1.format(new Date());
// 21-7-29 下午1:29
System.out.println(date1);
DateFormat df2 = DateFormat.getDateTimeInstance(dataStyle, timeStyle, englishLocale);
String date2 = df2.format(new Date());
// 7/29/21 1:31 PM
System.out.println(date2);
String str2 = "09-11-28 上午10时25分39秒 CST";
//实例化DateFormat工具类
DateFormat df3 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, Locale.getDefault());
Date d = df3.parse(str2);
System.out.println(d);
}
}
国际化货币
import java.text.NumberFormat;
import java.util.Locale;
public class CurrencyDemo1 {
//国际化数值
public static void main(String[] args) throws Exception {
//模拟语言环境
Locale locale = Locale.CHINA;
NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
//国际化货币
String m = nf.format(10000000);
//¥10,000,000.00
System.out.println(m);
}
}
import java.text.NumberFormat;
import java.util.Locale;
public class CurrencyDemo2 {
public static void main(String[] args) throws Exception {
//模拟语言环境
Locale locale = Locale.US;
//数据准备
double number = 1010101010;
//工具类
NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
//国际化货币
String m = nf.format(number);
// $1,010,101,010.00
System.out.println(m);
}
}