Java 国际化字符串
什么是国际化?
国际化(Internationalization,通常简称为i18n,因为在"i"和"n"之间有18个字母)是指设计软件时考虑到不 同国家、地区的用户,使其能够适应不同的语言和地区而无需修改程序本身。对于全球性的应用程序,国际化是一个非常重要的考虑因素。
Java提供了完善的国际化支持,使开发人员能够轻松创建适用于不同语言和地区的应用程序。
为什么需要国际化?
国际化的重要性
- 扩大用户群 - 支持多种语言可以帮助你的应用程序覆盖更多的用户
- 提升用户体验 - 用户能够以自己熟悉的语言使用应用程序
- 符合地区法规 - 某些国家可能要求软件提供当地语言版本
Java 国际化的核心组件
1. Locale类
Locale
类表示特定的地理、政治或文化区域。创建Locale
对象是进行国际化的第一步。
// 创建不同的Locale对象
Locale usLocale = new Locale("en", "US"); // 美国英语
Locale frLocale = new Locale("fr", "FR"); // 法国法语
Locale zhLocale = new Locale("zh", "CN"); // 中国中文
// 获取系统默认的Locale
Locale defaultLocale = Locale.getDefault();
System.out.println("系统默认的Locale: " + defaultLocale.getDisplayName());
// 获取所有可用的Locale
Locale[] availableLocales = Locale.getAvailableLocales();
System.out.println("可用的Locale数量: " + availableLocales.length);
2. ResourceBundle类
ResourceBundle
是Java国际化的核心 类,它管理特定于区域的资源,最常见的是用于管理不同语言的字符串。
创建属性文件
首先,我们需要为每种支持的语言创建属性文件:
messages_en_US.properties
(美国英语)
greeting=Hello
farewell=Goodbye
inquiry=How are you?
messages_fr_FR.properties
(法国法语)
greeting=Bonjour
farewell=Au revoir
inquiry=Comment allez-vous?
messages_zh_CN.properties
(中国中文)
greeting=您好
farewell=再见
inquiry=您好吗?
使用ResourceBundle加载资源
import java.util.Locale;
import java.util.ResourceBundle;
public class I18nDemo {
public static void main(String[] args) {
// 创建不同的Locale对象
Locale usLocale = new Locale("en", "US");
Locale frLocale = new Locale("fr", "FR");
Locale zhLocale = new Locale("zh", "CN");
// 加载美国英语资源
ResourceBundle bundle = ResourceBundle.getBundle("messages", usLocale);
System.out.println("美国英语: " + bundle.getString("greeting"));
// 加载法国法语资源
bundle = ResourceBundle.getBundle("messages", frLocale);
System.out.println("法国法语: " + bundle.getString("greeting"));
// 加载中国中文资源
bundle = ResourceBundle.getBundle("messages", zhLocale);
System.out.println("中国中文: " + bundle.getString("greeting"));
}
}
输出结果:
美国英语: Hello
法国法语: Bonjour
中国中文: 您好
3. MessageFormat类
MessageFormat
类帮助构建包含动态值的消息字符串。这对于构建复杂的国际化消息非常有用。
首先,让我们扩展我们的属性文件:
messages_en_US.properties
welcome=Welcome, {0}!
items=You have {0} {1} in your cart.
dateFormat=MM/dd/yyyy
messages_fr_FR.properties
welcome=Bienvenue, {0}!
items=Vous avez {0} {1} dans votre panier.
dateFormat=dd/MM/yyyy
messages_zh_CN.properties
welcome=欢迎,{0}!
items=您的购物车中有{0}件{1}。
dateFormat=yyyy年MM月dd日
现在我们可以使用MessageFormat
来格式化这些消息:
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MessageFormatDemo {
public static void main(String[] args) {
// 使用不同的Locale
Locale[] locales = {
new Locale("en", "US"),
new Locale("fr", "FR"),
new Locale("zh", "CN")
};
String username = "Alex";
int itemCount = 5;
String itemName = "book";
Date currentDate = new Date();
for (Locale locale : locales) {
// 加载对应语言的资源包
ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
// 格式化欢迎消息
String welcome = bundle.getString("welcome");
String formattedWelcome = MessageFormat.format(welcome, username);
// 格式化商品消息
String items = bundle.getString("items");
String formattedItems = MessageFormat.format(items, itemCount, itemName);
// 格式化日期
String datePattern = bundle.getString("dateFormat");
SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern, locale);
String formattedDate = dateFormat.format(currentDate);
// 打印结果
System.out.println("语言: " + locale.getDisplayLanguage());
System.out.println(formattedWelcome);
System.out.println(formattedItems);
System.out.println("当前日期: " + formattedDate);
System.out.println("------------------------");
}
}
}
输出结果可能类似于:
语言: 英语
Welcome, Alex!
You have 5 book in your cart.
当前日期: 07/15/2023
------------------------
语言: 法语
Bienvenue, Alex!
Vous avez 5 book dans votre panier.
当前日期: 15/07/2023
------------------------
语言: 中文
欢迎,Alex!
您的购物车中有5件book。
当前日期: 2023年07月15日
------------------------