Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
How to use one ResourceBundleProvider for all resource bundles in all Modules?

I am developing a multi-module Java application. Each module contains several resource bundles: when a class Demo.java requires translations, a resource bundle Demo.properties in the same package is created. As the application is not supposed to crash when a translation is missing, I want the translation key to be used as a fallback.

Since Java 9, a ResourceBundleProvider should be the right way to implement custom resource bundles:

package my.app.core;

public class KeyFallbackResourceBundleProvider extends AbstractResourceBundleProvider {

    @Override
    public ResourceBundle getBundle(String baseName, Locale locale) {
        var bundle = super.getBundle(baseName, locale);
        return new ResourceBundle() {

            @Override
            protected Object handleGetObject(String key) {
                try {
                    return bundle.getObject(key);
                } catch (MissingResourceException e) {
                    return key;
                }
            }

            @Override
            public Enumeration<String> getKeys() {
                return bundle.getKeys();
            }
        };
    }
}

Now, I need ResourceBundle#getBundle(String) to use that provider in every class that loads a resource bundle. It seems, however, that the name the provider is supposed to have depends on the name of the bundle to load, which is not acceptable for my use case. In addition, I want to use the same provider for all modules (which can directly depend on it).

Is there a way of achieving this without having to implement a ResourceBundleProvider for every resource bundle? And if not, is there any other way to implement the key fallback mechanism I described?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!