TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
资源包包含特定于语言环境的对象。当程序需要一个特定于语言环境的资源时,程序可以从适合当前用户语言环境的资源包中加载它。使用这种方式,可以编写很大程度上独立于用户语言环境的程序代码,它将资源包中大部分(即便不是全部)特定于语言环境的信息隔离开来。 这使编写的程序可以: 轻松地本地化或翻译成不同的语言
一次处理多个语言环境
以后可以轻松进行修改,以便支持更多的语言环境 - import java.util.Locale;
- import java.util.ResourceBundle;
- import java.util.Set;
-
- public class RBPropDemo {
- public static void main(String[] args) {
- ResourceBundle.clearCache();
- String bundleName = "MyResources";//资源包名
-
- ResourceBundle myResources = ResourceBundle.getBundle(bundleName, Locale.GERMAN);
-
- System.out.println("Key"s values:");
- System.out.println(myResources.getString("okKey"));
- System.out.println(myResources.getString("cancelKey"));
- System.out.println(myResources.getString("submitKey"));
- System.out.println("
- Checking okKey in resource bundle:");
- if (myResources.containsKey("okKey")) {
- System.out.println("okKey exists! " + " Value = " + myResources.getString("okKey"));
- } else {
- System.out.println("The key Doesn"t Exist");
- }
-
- System.out.println("
- Get a set of keys:");
- Set< String> keySet = myResources.keySet();
- Object[] keys = keySet.toArray();
- for (int i = 0; i < keys.length; i++) {
- System.out.println("Key " + (i + 1) + " = " + keys[i]);
- }
- }
- }
复制代码 /*
MyResources.properties file 缺省的资源文件
okKey = OK
cancELKey = Cancel
submitKey = Submit
The MyResources_de.properties file//德语资源文件 cancelKey = Abbrechen
*/ 运行结果:
C:java>java RBPropDemo
Key"s values:
OK
Abbrechen
Submit Checking okKey in resource bundle:
okKey exists! Value = OK Get a set of keys:
Key 1 = okKey
Key 2 = submitKey
Key 3 = cancelKey |
|