How to configure ResourceBundle "no fallback control" in Java 9
Asked Answered
S

3

8

As answered in How to ignore the system default Locale to retrieve resourceBundle you can configure in Java 8 or older to not fallback to default locale via:

ResourceBundle.getBundle("MyResources",
  new Locale("en", "US"),
  ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_PROPERTIES))

In Java 9, ResourceBundle.Control usage throws an UnsupportedOperationException when used in Named Modules: ResourceBundle.Control is not supported in named modules.

How do I implement/configure a custom "MyResourcesProvider[Impl]" to achieve the same behaviour as the ResourceBundle.Control.getNoFallbackControl provided?

Sallyanne answered 24/5, 2017 at 20:2 Comment(0)
C
0

You need to implements a java.util.spi.ResourceBundleProvider and register it as service for the base name.

The getBundle(String, Locale) should return null if no resource is available.

The problem is that you need to register for every bundle base name its own provider.

Cromagnon answered 29/1, 2024 at 15:23 Comment(0)
C
0

You can load it directly over:

Control.newBundle( baseName, locale, "java.properties", loader, false );

But then you need to implements the caching self.

Cromagnon answered 29/1, 2024 at 15:46 Comment(0)
M
-1

Are you using UTF8, instead of ISO-8859-1? Because Java 8 and before used it for ResourceBundle and properties files.

See: http://openjdk.java.net/jeps/226

Moreville answered 10/7, 2018 at 15:18 Comment(6)
Encoding doesn't matter here: an UnsupportedOperationException is the issue.Sallyanne
To whoever downvoted my answer - could you please tell me what was wrong with it? Or did you just downvoted it because you thought it wasn't a solution for @Sallyanne problem?Moreville
@Sallyanne For example, if you invoke URLEncoder.encode(value, "UTF-8");, you can get UnsupportedOperationException for unsupported encoding.Moreville
You are making it even worse. UnsupportedEncodingException it is -- but what has it do with my question? Right. Nothing. I downvoted it because "the answer is not useful".Sallyanne
It's not UnsupportedEncodingException, it's UnsupportedOperationException.Moreville
I thought of a correlation between you, saying that it stopped working after migrating to Java 9 and finding out a source that says properties file encoding changed in Java 9 (from here openjdk.java.net/jeps/226) and because I remembered that using wrong encodings often resulted in UnsupportedOperationException.Moreville

© 2022 - 2025 — McMap. All rights reserved.