Eclipse 没有看到 java 文件属性

Eclipse not seeing java file properties

我在 Java 中进行国际化。

我创建了两个文件:MessageBundle_en_US.propertiesMessageBundle_en_IN.properties

MessageBundle_en_US.properties:

greeting=Hello, how are you?

MessageBundle_en_IN.properties

greeting=Halo, apa

最后,这是主要的 class:

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 文件放在与主 class 相同的目录中,正如评论中建议的那样,你应该调整像这样调用文件:

projectName/propertiesFileName

这就是全部。

  1. 将您的 *.properties 文件放在与 InternationalizationDemo.java
  2. 相同的文件夹中
  3. 重命名:MessageBundle_en_IN.properties 为:MessageBundle_in_ID.properties