Java: 无法定位具有区域设置的资源文件

huangapple go评论71阅读模式
英文:

Java: Unable to locate the resource file with locales

问题

以下是您要翻译的代码部分:

package com.my.exercise;

import java.util.Locale;
import java.util.ResourceBundle;

public class Main {
    public static void main(String[] args) {
        String language = "en";
        String country = "US";

        Locale l = new Locale(language, country);
        ResourceBundle r = ResourceBundle.getBundle("Bundle", l);

        String str = r.getString("wish"); // Prints "Happy Birthday"
        System.out.println(str);
    }
}

如需进行进一步交流或有其他疑问,请随时提问。

英文:

The following code

package com.my.exercise;

import java.util.Locale;
import java.util.ResourceBundle;

public class Main {
    public static void main(String[] args) {
        String language = "en";
        String country = "US";

        Locale l = new Locale(language, country);
        ResourceBundle r = ResourceBundle.getBundle("Bundle", l);

        String str = r.getString("wish"); // Prints "Happy Birthday"
        System.out.println(str);
    }
}

works for my sample application with the following folder structure when Resource Bundle 'Bundle' is located in src folder:

Java: 无法定位具有区域设置的资源文件

But the same code fails with error "Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name Bundle, locale en_US" when Resource Bundle 'Bundle' is placed inside src/main folder:

Java: 无法定位具有区域设置的资源文件

Problem: What do I need to modify in my code in order to make Resource Bundle 'Bundle' inside src/main folder to be discoverable?

答案1

得分: 0

非常感谢您的建议,VGR!事实上,后端有一个Maven项目。尽管在我的本地Idea项目中无法找到资源文件,我还是提交了代码,但它确实在服务器上通过了编译。我只是确保我使用了位于src/main文件夹中的资源包位置。

Java: 无法定位具有区域设置的资源文件

英文:

Thank you very much for your advise, VGR! Indeed, there was a Maven project in the back-end. I submitted the code despite it was unable to find the resource files in my local Idea project, but it did pass the compilation on the server. I just ensured that I used the Resource Bundle location within src/main folder.

Java: 无法定位具有区域设置的资源文件

huangapple
  • 本文由 发表于 2020年7月24日 09:07:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63065339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定