7

I have two modules: module-a and module-b. Module-a has properties file (com/foo/texts_en.properties). Module-a exports com.foo package. In module-b I want to get this resource. For this I do the following in module-b:

Module moduleA = ClassFromModuleA.class.getModule();
ResourceBundle resourceBundle = ResourceBundle.getBundle("com/foo/texts",
                Locale.ENGLISH, moduleA.getClassLoader());
System.out.println("TEST :" + resourceBundle.getString(key);

This is what I get:

Caused by: java.util.MissingResourceException: Can't find bundle for base name com/foo/texts, locale en
    at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2045)
    at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1679)
    at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1572)
    at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:1273)

What is my mistake?

Maxim
  • 52,561
  • 27
  • 155
  • 209
Pavel_K
  • 10,748
  • 13
  • 73
  • 186
  • Now that Java 9 has been released, you should link to the javadoc’s [permanent location](http://docs.oracle.com/javase/9/docs/api/) so the link will be valid for years to come. – VGR Sep 22 '17 at 18:33
  • @VGR I edited. My fault. – Pavel_K Sep 22 '17 at 18:37

2 Answers2

1

The "Resource Bundles in Named Modules" section of ResourceBundle provides the details on how resources in named modules are located.

Pavel_K
  • 10,748
  • 13
  • 73
  • 186
Alan Bateman
  • 5,283
  • 1
  • 20
  • 25
  • Do I understand right? - It is supposed to create one service implementation for `every` resource bundle? For example, I have in module-a `com/foo/texts_en.properties` and `com/boo/messages_en.properties`. Do I have to create two implementations of ResourceBundleProvider in module-a? – Pavel_K Sep 22 '17 at 17:54
  • You have several choices. You can leave the resources on the class path. You can put the resources into a module (which I think you are doing) but that module may not be resolved because no module requires it, hence you might need to use `--add-modules`. If you do this then you can open the packages to the consumer and RB will be able to locate the resources. Alternatively, you can deploy the resource bundles as service providers as per the javadoc. The latter takes a bit of work to setup but it means that service binding will ensure that the modules with the resources are resolved. – Alan Bateman Sep 23 '17 at 07:51
  • 3
    Your answer should quote the relevant section. Otherwise this seems more like a link only answer. – SpaceTrucker Sep 23 '17 at 09:10
  • I might be frank and biased but after reading all this and trying it out, I come to the conclusion that java modules and split-packages rendered the Java standard I18N mechanism pretty much useless. If Java does not improve here, library projects will not migrate to modules and if the Java ecosystem does not follow the vision of the module system failed. – Jörg Oct 31 '19 at 08:03
0

After trying out all options I came to the conclusion that the only reasonable way to solve this properly is to keep the localized resource bundles (*_«locale».properties) in a regular Jar rather than creating a named module. So simply create a *.jar file only containing the localized resource bundle properties and without any module-info.

I have added a note to the migration guide for Java9+ and JPMS:

https://github.com/devonfw/devon4j/blob/develop/documentation/guide-jdk.asciidoc#resourcebundles

Jörg
  • 822
  • 1
  • 10
  • 13