我在 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)
更新我已经找到了无法看到文件的原因,首先您需要将*properties文件放在与主类相同的目录中,就像人们在评论中建议的那样,您应该这样调整对文件的调用:
projectName/propertiesFileName
这就是全部。
1条答案
按热度按时间qaxu7uf21#
放置您的
*.properties
文件位于与InternationalizationDemo.java
重命名:MessageBundle_en_IN.properties
收件人:MessageBundle_in_ID.properties