我正在用Java进行国际化。
我已经创建了两个文件:MessageBundle_en_US.properties
和MessageBundle_en_IN.properties
。
MessageBundle_en_US.properties
:
greeting=Hello, how are you?
MessageBundle_en_IN.properties
greeting=Halo, apa
最后,这里是主要的类:
import java.util.*;
public class InternationalizationDemo {
public static void main(String[] args) {
ResourceBundle bundle =
ResourceBundle.getBundle("MessageBundle", Locale.US);
System.out.println(
"Message in "
+ Locale.US
+ ": "
+ bundle.getString("greeting")
);
//changing the default locale to indonasian
Locale.setDefault(new Locale("in", "ID"));
bundle = ResourceBundle.getBundle("MessageBundle");
System.out.println(
"Message in "
+ Locale.getDefault()
+ ": "
+ bundle.getString("greeting")
);
}
}
我收到以下错误堆栈:
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name MessageBundle, locale en_US
at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2055)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1689)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1593)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1556)
at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:932)
at provame.InternationalizationDemo.main(InternationalizationDemo.java:8)
UPDATE我已经找到了它无法看到文件的原因,首先你需要把*properties文件放在与主类相同的目录中,就像人们在评论中建议的那样,你应该这样调整对文件的调用:
projectName/propertiesFileName
这就是全部。
- 将
*.properties
文件与InternationalizationDemo.java
放在同一文件夹中 - 将:
MessageBundle_en_IN.properties
重命名为:MessageBundle_in_ID.properties